perf(backend): offload blocking calls in async paths to threads (#26381)
Audit of asyncio.sleep vs time.sleep and event-loop-blocking calls: - utils/plugin.py: run pip `install_frontmatter_requirements` (subprocess.check_call) via asyncio.to_thread in load_tool_module_by_id, load_function_module_by_id, and install_tool_and_function_dependencies. - retrieval/utils.py: move the synchronous SSRF-guarded requests probe and loader.load() in get_content_from_url into a sync helper run via asyncio.to_thread. - routers/audio.py: write uploaded audio to disk off the event loop in transcription(). - routers/pipelines.py: write uploaded pipeline file off the event loop in upload_pipeline(). The existing time.sleep call sites are all in genuinely synchronous functions (sync requests/DB drivers/daemon threads) with async counterparts that already use asyncio.sleep, so no time.sleep -> asyncio.sleep changes were needed. Claude-Session: https://claude.ai/code/session_01LXR5bYfsfSS42RGHQZu2Ta Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
c7e634776d
commit
51246bcb31
4 files changed
+37
-12
No files matched your search
@@ -1192,8 +1192,12 @@ async def transcription(
|
||||
if not os.path.realpath(file_path).startswith(os.path.realpath(file_dir)):
|
||||
raise ValueError('Invalid file path detected')
|
||||
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(contents)
|
||||
def _write_upload():
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(contents)
|
||||
|
||||
# Audio uploads can be large; write to disk off the event loop.
|
||||
await asyncio.to_thread(_write_upload)
|
||||
|
||||
try:
|
||||
metadata = None
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
@@ -236,9 +237,12 @@ async def upload_pipeline(
|
||||
|
||||
response = None
|
||||
try:
|
||||
# Save the uploaded file
|
||||
with open(file_path, 'wb') as buffer:
|
||||
shutil.copyfileobj(file.file, buffer)
|
||||
# Save the uploaded file off the event loop (uploads can be large).
|
||||
def _save_upload():
|
||||
with open(file_path, 'wb') as buffer:
|
||||
shutil.copyfileobj(file.file, buffer)
|
||||
|
||||
await asyncio.to_thread(_save_upload)
|
||||
|
||||
url, key = await get_openai_connection(urlIdx)
|
||||
|
||||
|
||||
Reference in new issue
Block a user