mirror of
https://github.com/crewAIInc/crewAI.git
synced 2025-12-16 04:18:35 +00:00
Fix lint issues in asyncio_utils.py
- Remove trailing whitespace from blank lines - Remove unused loop variable Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -1,37 +1,37 @@
|
||||
"""Utilities for handling asyncio operations safely across different contexts."""
|
||||
|
||||
import asyncio
|
||||
from typing import Any, Coroutine
|
||||
from collections.abc import Coroutine
|
||||
from typing import Any
|
||||
|
||||
|
||||
def run_coroutine_sync(coro: Coroutine) -> Any:
|
||||
"""
|
||||
Run a coroutine synchronously, handling both cases where an event loop
|
||||
is already running and where it's not.
|
||||
|
||||
|
||||
This is useful when you need to run async code from sync code, but you're
|
||||
not sure if you're already in an async context (e.g., when using asyncio.to_thread).
|
||||
|
||||
|
||||
Args:
|
||||
coro: The coroutine to run
|
||||
|
||||
|
||||
Returns:
|
||||
The result of the coroutine
|
||||
|
||||
|
||||
Raises:
|
||||
Any exception raised by the coroutine
|
||||
"""
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
return asyncio.run(coro)
|
||||
else:
|
||||
import concurrent.futures
|
||||
import threading
|
||||
|
||||
|
||||
result = None
|
||||
exception = None
|
||||
|
||||
|
||||
def run_in_new_loop():
|
||||
nonlocal result, exception
|
||||
try:
|
||||
@@ -43,11 +43,11 @@ def run_coroutine_sync(coro: Coroutine) -> Any:
|
||||
new_loop.close()
|
||||
except Exception as e:
|
||||
exception = e
|
||||
|
||||
|
||||
thread = threading.Thread(target=run_in_new_loop)
|
||||
thread.start()
|
||||
thread.join()
|
||||
|
||||
|
||||
if exception:
|
||||
raise exception
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user