Convert async functions to sync and vice versa.
Project description
Desync
Simple conversions between synchronous and asynchronous functions and function
calls. You can use it to wait (really block) for asynchronous function results
with sync
or create synchronous versions with synced
:
# Assume async_func is asynchronous.
async_func: Callable[..., Awaitable[T]]
# Call async_func and wait for its result synchronously:
result: T = sync(async_func, *args, **kwargs)
# Create a version which is synchronous without calling it:
desynced_func: Callable[..., T] = synced(async_func)
You can also wait for synchronous functions via desync
or create asynchronous
versions with desynced
:
# Assume sync_func is synchronous.
func: Callable[..., T]
# Call sync_func and wait for its result in an asynchronous context:
awaitable: Awaitable[T] = desync(sync_func, *args, **kwargs)
result: T = await awaitable
# Create a version which is asynchronous without calling it:
descyned_func: Callable[..., Awaitable[T]] = desynced(func)
Desynced functions run in parallel (as far as python can run in parallel).
def wait_some():
time.sleep(1)
return 1
# This should take approx. 1 second:
assert 10 == sum(
await asyncio.gather(*(
desync(wait_some) for i in range(10)
))
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydesync-0.1.2.tar.gz
(7.2 kB
view hashes)