mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
fix: fastapi no thread pool (#1303)
This commit is contained in:
@@ -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('/')
|
||||
|
||||
Reference in New Issue
Block a user