Is it likely that this library will be blocking code execution?
How do I know if it will cause problems?
You should not allow to having long-running blocking (synchronous) code inside any of your coroutines. It’ll lead to blocking your global event loop and further blocking all of your coroutines everywhere.
async def main(): await asyncio.sleep(3) # async sleeping, it's ok time.sleep(3) # synchronous sleeping, this freezes event loop # and all coroutines for 3 seconds, # you should avoid it! await asyncio.sleep(3) # async sleeping, it's ok
If you need to run blocking code inside coroutine you should do it in executor (read here about it).
You should keep it in mind when you writing coroutines, but usually asyncio will warn you about this error if you’ll enable debug mode:
import asyncio import time async def main(): await asyncio.sleep(3) time.sleep(3) await asyncio.sleep(3) loop = asyncio.get_event_loop() loop.set_debug(True) # debug mode try: loop.run_until_complete(main()) finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
You’ll see warning:
Executing <Handle <TaskWakeupMethWrapper object at 0x000002063C2521F8>(<Future finis...events.py:275>) created at C:\Users\gmn\AppData\Local\Programs\Python\Python36\Lib\asyncio\futures.py:348> took 3.000 seconds
Read more here: Source link