Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of Python code to run a slow call to several resources asynchronously and execute some callback function on return
import asyncio
def worker_that_waits_for_network_io(data, ...):
...
return result
def callback_hook(retrieved_data, ...):
...
async def async_worker(data, ...)
loop = asyncio.get_event_loop()
retrieved_data = await loop.run_in_executor(None, worker_that_waits_for_network_io, data, ...)
return callback_hook(retrieved_data)
all_data = [
("url1.com", ...),
("url2.com", ...),
("database3", ...),
("url4.com", ...),
]
loop = asyncio.get_event_loop()
results = asyncio.run(asyncio.gather(*(async_worker(*data_line) for data_line in all_data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment