1
1
# SPDX-License-Identifier: Apache-2.0
2
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
3
4
+ import asyncio
5
+ import atexit
6
+ from concurrent .futures import ThreadPoolExecutor
4
7
from itertools import groupby
5
8
from pathlib import Path
6
9
from typing import TYPE_CHECKING , Any , Optional , TypeVar , Union
33
36
MultiModalKwargs = Any
34
37
MultiModalPlaceholderDict = Any
35
38
39
+ global_thread_pool = ThreadPoolExecutor (
40
+ max_workers = envs .VLLM_MEDIA_LOADING_THREAD_COUNT )
41
+ atexit .register (global_thread_pool .shutdown )
42
+
36
43
37
44
class MediaConnector :
38
45
@@ -139,19 +146,26 @@ async def load_from_url_async(
139
146
fetch_timeout : Optional [int ] = None ,
140
147
) -> _M :
141
148
url_spec = urlparse (url )
149
+ loop = asyncio .get_running_loop ()
142
150
143
151
if url_spec .scheme .startswith ("http" ):
144
152
connection = self .connection
145
153
data = await connection .async_get_bytes (url , timeout = fetch_timeout )
146
-
147
- return media_io .load_bytes (data )
154
+ future = loop .run_in_executor (global_thread_pool ,
155
+ media_io .load_bytes , data )
156
+ return await future
148
157
149
158
if url_spec .scheme == "data" :
150
- return self ._load_data_url (url_spec , media_io )
159
+ future = loop .run_in_executor (global_thread_pool ,
160
+ self ._load_data_url , url_spec ,
161
+ media_io )
162
+ return await future
151
163
152
164
if url_spec .scheme == "file" :
153
- return self ._load_file_url (url_spec , media_io )
154
-
165
+ future = loop .run_in_executor (global_thread_pool ,
166
+ self ._load_file_url , url_spec ,
167
+ media_io )
168
+ return await future
155
169
msg = "The URL must be either a HTTP, data or file URL."
156
170
raise ValueError (msg )
157
171
@@ -489,4 +503,4 @@ def fetch_video(
489
503
"video" : video_io_kwargs
490
504
}
491
505
media_connector = MediaConnector (media_io_kwargs = media_io_kwargs )
492
- return media_connector .fetch_video (video_url )
506
+ return media_connector .fetch_video (video_url )
0 commit comments