Skip to content

Instantly share code, notes, and snippets.

@jsbueno
Created July 24, 2020 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbueno/2718c8b8e897ba539f5f46011ec93fdb to your computer and use it in GitHub Desktop.
Save jsbueno/2718c8b8e897ba539f5f46011ec93fdb to your computer and use it in GitHub Desktop.
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