mirror of
https://github.com/ankitects/anki.git
synced 2025-11-06 12:47:11 -05:00
Simplify async request handling
This commit is contained in:
parent
9dcbf70aeb
commit
3c8f35efd3
1 changed files with 2 additions and 13 deletions
|
|
@ -680,7 +680,7 @@ AsyncRequestReturnType = TypeVar("AsyncRequestReturnType")
|
|||
class AsyncRequestHandler(Generic[AsyncRequestReturnType]):
|
||||
def __init__(self, callback: Callable[[AsyncRequestHandler], None]) -> None:
|
||||
self.callback = callback
|
||||
self.loop = asyncio.get_event_loop()
|
||||
self.loop = asyncio.get_running_loop()
|
||||
self.future = self.loop.create_future()
|
||||
|
||||
def run(self) -> None:
|
||||
|
|
@ -1075,18 +1075,7 @@ def _extract_collection_post_request(path: str) -> DynamicRequest | NotFound:
|
|||
import inspect
|
||||
|
||||
if inspect.iscoroutinefunction(handler):
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
if loop.is_running():
|
||||
import concurrent.futures
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
||||
future = executor.submit(asyncio.run, handler())
|
||||
data = future.result()
|
||||
else:
|
||||
data = loop.run_until_complete(handler())
|
||||
except RuntimeError:
|
||||
data = asyncio.run(handler())
|
||||
data = asyncio.run(handler())
|
||||
else:
|
||||
result = handler()
|
||||
data = result
|
||||
|
|
|
|||
Loading…
Reference in a new issue