Created
July 24, 2020 18:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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