Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Chainlit_chainlit /backend /chainlit /sync.py
| import asyncio | |
| import threading | |
| from typing import Any, Coroutine, ParamSpec, TypeVar | |
| from asyncer import asyncify | |
| from syncer import sync | |
| from chainlit.context import context_var | |
| make_async = asyncify | |
| T_Retval = TypeVar("T_Retval") | |
| T_ParamSpec = ParamSpec("T_ParamSpec") | |
| T = TypeVar("T") | |
| def run_sync(co: Coroutine[Any, Any, T_Retval]) -> T_Retval: | |
| """Run the coroutine synchronously.""" | |
| # Copy the current context | |
| current_context = context_var.get() | |
| # Define a wrapper coroutine that sets the context before running the original coroutine | |
| async def context_preserving_coroutine(): | |
| # Set the copied context to the coroutine | |
| context_var.set(current_context) | |
| return await co | |
| # Execute from the main thread in the main event loop | |
| if threading.current_thread() == threading.main_thread(): | |
| return sync(context_preserving_coroutine()) | |
| else: # Execute from a thread in the main event loop | |
| result = asyncio.run_coroutine_threadsafe( | |
| context_preserving_coroutine(), loop=current_context.loop | |
| ) | |
| return result.result() | |
Xet Storage Details
- Size:
- 1.12 kB
- Xet hash:
- c5a8fcd0ec08794bc4ff298baf608c23e2798c24a2c74eede5de3af01c970506
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.