Created
March 18, 2020 21:20
-
-
Save vlad-bezden/da4eb94989354941ab93de9ec4228669 to your computer and use it in GitHub Desktop.
Example of running blocking code in asyncio using default executor. There are four tasks each of them sleep from 1 to 4 seconds corresponding. If it were concurrent call then they would run for 1 + 2+ 3 + 4 = 10 seconds. However, running them in executor executes each task on separate thread and the total time to execute all of them took 4 secon…
This file contains hidden or 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 | |
from time import sleep | |
import logging | |
logging.basicConfig( | |
level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s" | |
) | |
def long_task(t): | |
"""Simulate long IO bound task.""" | |
logging.info("2. t: %s", t) | |
sleep(t) | |
logging.info("4. t: %s", t) | |
return t ** 2 | |
async def main(): | |
loop = asyncio.get_running_loop() | |
inputs = range(1, 5) | |
logging.info("1.") | |
futures = [loop.run_in_executor(None, long_task, i) for i in inputs] | |
logging.info("3.") | |
results = await asyncio.gather(*futures) | |
logging.info("5.") | |
for (i, result) in zip(inputs, results): | |
logging.info("6. Result: %s, %s", i, result) | |
if __name__ == "__main__": | |
asyncio.run(main()) | |
# Output | |
# 2020-03-18 17:13:07,523 23964 main 1. | |
# 2020-03-18 17:13:07,524 5008 long_task 2. t: 1 | |
# 2020-03-18 17:13:07,525 21232 long_task 2. t: 2 | |
# 2020-03-18 17:13:07,525 22048 long_task 2. t: 3 | |
# 2020-03-18 17:13:07,526 25588 long_task 2. t: 4 | |
# 2020-03-18 17:13:07,526 23964 main 3. | |
# 2020-03-18 17:13:08,526 5008 long_task 4. t: 1 | |
# 2020-03-18 17:13:09,526 21232 long_task 4. t: 2 | |
# 2020-03-18 17:13:10,527 22048 long_task 4. t: 3 | |
# 2020-03-18 17:13:11,527 25588 long_task 4. t: 4 | |
# 2020-03-18 17:13:11,527 23964 main 5. | |
# 2020-03-18 17:13:11,528 23964 main 6. Result: 1, 1 | |
# 2020-03-18 17:13:11,528 23964 main 6. Result: 2, 4 | |
# 2020-03-18 17:13:11,529 23964 main 6. Result: 3, 9 | |
# 2020-03-18 17:13:11,529 23964 main 6. Result: 4, 16 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment