fix: fastapi no thread pool (#1303)

This commit is contained in:
Ran Shemtov
2025-02-04 18:17:34 +01:00
committed by GitHub
parent 65259e808a
commit 7114d92b5e
2 changed files with 9 additions and 4 deletions
@@ -26,10 +26,13 @@ def add_fastapi_endpoint(
prefix: str,
*,
max_workers: int = 10,
use_thread_pool: bool = True,
):
"""Add FastAPI endpoint with configurable ThreadPoolExecutor size"""
executor = ThreadPoolExecutor(max_workers=max_workers)
print(f"use_thread_pool {use_thread_pool}")
def run_handler_in_thread(request: Request, sdk: CopilotKitRemoteEndpoint):
# Run the handler coroutine in the event loop
loop = asyncio.new_event_loop()
@@ -37,9 +40,11 @@ def add_fastapi_endpoint(
return loop.run_until_complete(handler(request, sdk))
async def make_handler(request: Request):
loop = asyncio.get_event_loop()
future = loop.run_in_executor(executor, run_handler_in_thread, request, sdk)
return await future
if use_thread_pool:
loop = asyncio.get_event_loop()
future = loop.run_in_executor(executor, run_handler_in_thread, request, sdk)
return await future
return await handler(request, sdk)
# Ensure the prefix starts with a slash and remove trailing slashes
normalized_prefix = '/' + prefix.strip('/')