async_sync¶
-
privex.helpers.asyncx.
async_sync
(f)[source]¶ Async Synchronous Decorator, borrowed from https://stackoverflow.com/a/23036785/2648583 - added this PyDoc comment and support for returning data from a synchronous function
Allows a non-async function to run async functions using
yield from
- and can also return dataUseful for unit testing, since unittest.TestCase functions are synchronous.
Example async function:
>>> async def my_async_func(a, b, x=None, y=None): ... return a, b, x, y ...
Using the above async function with a non-async function:
>>> @async_sync ... def sync_function(): ... result = yield from my_async_func(1, 2, x=3, y=4) ... return result ... >>> r = sync_function() >>> print(r) (1, 2, 3, 4,) >>> print(r[1]) 2