Skip to content

Instantly share code, notes, and snippets.

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 = [
("", ...),
("", ...),
("database3", ...),
("", ...),
loop = asyncio.get_event_loop()
results =*(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