Using Python’s Asyncio to Asynchronously Run Existing Blocking Code in 2020

TL;DR Code Snippet for Python >=3.7: import asyncio import time def existing_blocking_method(): print(‘going to sleep’) time.sleep(2) print(‘waking up’) async def main(): loop = asyncio.get_running_loop() # Only exists in python 3.7 await loop.run_in_executor(None, lambda: existing_blocking_method()) # These run consecutively, as run_in_executor returns a Future, which is a callback that eventually returns the result. await loop.run_in_executor(None, lambda: … Read moreUsing Python’s Asyncio to Asynchronously Run Existing Blocking Code in 2020