API Reference¶
- greenletio.async_(fn=None, *, with_context=False)¶
Convert a standard function to an async function that can be awaited.
This function creates an async wrapper for a standard function, allowing callers to invoke the function as an awaitable. Example:
def fn(): pass async def main(): await async_(fn)
It is also possible to use this function as a decorator:
@async_ def fn(): pass async def main(): await fn()
If you are using context variables, you can use the
with_context
option to ensure that the context is preserved when the function is called:@async_(with_context=True) def fn(): pass
- Parameters:
fn – the standard function to convert to async.
- greenletio.await_(coro_or_fn)¶
Wait for an async function to complete in a standard function, without blocking the asyncio loop.
This function can be used in two ways. First, as a replacement to the
await
keyword in a standard function:def func(): await_(asyncio.sleep(1))
Second, as a decorator to an async function, so that the function can be called as a standard function while still not blocking the asyncio loop:
@await_ async def func(): await asyncio.sleep(1) def main(): func()
- Parameters:
coro_or_fn – The coroutine or future to await, or when used as a decorator, the async function to decorate.
- greenletio.patch_blocking(modules=None)¶
Context manager that replaces blocking modules from the Python standard library with non-blocking replacements.
The following example shows how to import the
requests
package so that it becomes non-blocking:with patch_blocking(): import requests
- Parameters:
modules – the list of modules to patch, or None to patch all the supported modules, which at this time are
socket
,select
,selectors
,ssl
,threading
andtime
.
- greenletio.patch_psycopg2()¶
Patch the
psycopg2
package so that it becomes non-blocking.