run_coro_thread_async¶
-
async
privex.helpers.asyncx.
run_coro_thread_async
(func: callable, *args, _queue_timeout=30.0, _queue_sleep=0.05, **kwargs) → Any[source]¶ AsyncIO version of
run_coro_thread()
which usesasyncio.sleep()
while waiting on a result from the queue, allowing you to run multiple AsyncIO coroutines which call blocking synchronous code - simultaneously, e.g. by usingasyncio.gather()
Below is an example of running an example coroutine
hello
which runs the synchronous blockingtime.sleep
. Usingrun_coro_thread_async()
plusasyncio.gather()
- we can runhello
4 times simultaneously, despite the use of the blockingtime.sleep()
.Basic usage:
>>> import asyncio >>> from privex.helpers.asyncx import run_coro_thread_async >>> async def hello(world): ... time.sleep(1) ... return world * 10 >>> await asyncio.gather(run_coro_thread_async(hello, 5), run_coro_thread_async(hello, 15), ... run_coro_thread_async(hello, 90), run_coro_thread_async(hello, 25)) [50, 150, 900, 250]
- Parameters
func (callable) – A reference to the
async def
coroutine function that you want to runargs – Positional arguments to pass-through to the coroutine function
kwargs – Keyword arguments to pass-through to the coroutine function
_queue_timeout (float|int) – (default:
30
) Maximum amount of seconds to wait for a result or exception fromfunc
before giving up._queue_sleep – (default:
0.05
) Amount of time to AsyncIO sleep between each check of the result queue
- Return Any coro_res
The result returned from the coroutine
func