Skip to content

Commit 6827d46

Browse files
author
sangchengmeng
committed
[add]support req-disconnected
1 parent b437a74 commit 6827d46

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

lightllm/server/api_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ async def tokens(request: Request):
368368

369369
multimodal_params_dict = request_dict.get("multimodal_params", {})
370370
multimodal_params = MultimodalParams(**multimodal_params_dict)
371-
await multimodal_params.verify_and_preload()
371+
await multimodal_params.verify_and_preload(request)
372372
return JSONResponse(
373373
{
374374
"ntokens": g_objs.httpserver_manager.tokens(

lightllm/server/httpserver/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ async def generate(
228228
original_multimodal_params = copy.deepcopy(multimodal_params)
229229

230230
if self.pd_mode.is_P_or_NORMAL():
231-
await multimodal_params.verify_and_preload()
231+
await multimodal_params.verify_and_preload(request)
232232

233233
# 记录请求到达的相关信息
234234
await self._log_req_header(request_headers, group_request_id)

lightllm/server/multimodal_params.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from PIL import Image
77
from lightllm.utils.image_utils import fetch_image
88
import base64
9+
from fastapi import Request
910

1011

1112
class ImageItem:
@@ -24,11 +25,11 @@ def __init__(self, **kwargs):
2425
self._preload_data = None
2526
self.extra_params = {}
2627

27-
async def preload(self):
28+
async def preload(self, request: Request):
2829
try:
2930
if self._type == "url":
3031
timeout = int(os.getenv("REQUEST_TIMEOUT", "5"))
31-
img_data = await fetch_image(self._data, timeout=timeout)
32+
img_data = await fetch_image(self._data, request, timeout=timeout)
3233
elif self._type == "base64":
3334
img_data = base64.b64decode(self._data)
3435
elif self._type == "image_size":
@@ -81,9 +82,9 @@ def __init__(
8182
self.images = [ImageItem(**i) for i in images]
8283
return
8384

84-
async def verify_and_preload(self):
85+
async def verify_and_preload(self, request: Request):
8586
for image in self.images:
86-
await image.preload()
87+
await image.preload(request)
8788
return
8889

8990
def to_dict(self):

lightllm/utils/image_utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import time
12
import base64
23
import httpx
4+
import logging
35
from PIL import Image
46
from io import BytesIO
7+
from fastapi import Request
8+
from lightllm.utils.log_utils import init_logger
9+
10+
logger = init_logger(__name__)
511

612

713
def image2base64(img_str: str):
@@ -13,17 +19,23 @@ def image2base64(img_str: str):
1319
return base64.b64encode(buffer.getvalue()).decode("utf-8")
1420

1521

16-
async def fetch_image(url, timeout):
22+
async def fetch_image(url, request: Request, timeout):
23+
logger.info(f"Begin to download image from url: {url}")
24+
start_time = time.time()
1725
async with httpx.AsyncClient() as client:
1826
async with client.stream("GET", url, timeout=timeout) as response:
1927
response.raise_for_status()
2028
ans_bytes = []
21-
2229
async for chunk in response.aiter_bytes(chunk_size=1024 * 1024):
30+
if await request.is_disconnected():
31+
await response.aclose()
32+
raise Exception("Request disconnected. User cancelled download.")
2333
ans_bytes.append(chunk)
2434
# 接收的数据不能大于128M
2535
if len(ans_bytes) > 128:
26-
raise Exception("image data is too big")
36+
raise Exception("Image data is too big")
2737

2838
content = b"".join(ans_bytes)
29-
return content
39+
end_time = time.time()
40+
logger.info("Download image time: {:.2f} seconds".format(end_time - start_time))
41+
return content

0 commit comments

Comments
 (0)