Created
November 5, 2021 11:48
-
-
Save zoni/6cc98558c3be230714b209477b23f0e3 to your computer and use it in GitHub Desktop.
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 | |
import os | |
from pprint import pprint | |
async def do_work(): | |
"""Schedule and run a bunch of tasks that do nothing but sleep""" | |
tasks = [asyncio.create_task(asyncio.sleep(duration)) for duration in [0, 1, 3, 5]] | |
results, _ = await asyncio.wait(tasks) | |
return results | |
def main(): | |
try: | |
# If there is an existing event loop, schedule the coroutine do_work on | |
# it and return its result. | |
loop = asyncio.get_running_loop() | |
results = asyncio.run_coroutine_threadsafe(do_work, loop).result() | |
except RuntimeError: | |
# Otherwise, start a new event loop ourselves. | |
results = asyncio.run(do_work()) | |
pprint(results) | |
if __name__ == "__main__": | |
if os.environ["RUN_ASYNC"].lower() == "true": | |
print("Running asynchronously") | |
loop = asyncio.new_event_loop() | |
main() | |
# This will run forever because there's no (easy) way to signal the | |
# tasks scheduled in main() have actually run and completed, but for | |
# demo purposes, this is fine. | |
loop.run_forever() | |
else: | |
print("Running synchronously") | |
main() |
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
➔ export RUN_ASYNC=false; time python foo.py | |
Running synchronously | |
{<Task finished name='Task-2' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-3' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-4' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-5' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>} | |
python foo.py 0.05s user 0.00s system 0% cpu 5.050 total | |
➔ export RUN_ASYNC=true; time python foo.py | |
Running asynchronously | |
{<Task finished name='Task-2' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-3' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-4' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>, | |
<Task finished name='Task-5' coro=<sleep() done, defined at /usr/lib/python3.9/asyncio/tasks.py:636> result=None>} | |
^CTraceback (most recent call last): | |
File "/home/zoni/Workspace/castoredc/castoredc-api-review/foo.py", line 33, in <module> | |
loop.run_forever() | |
File "/usr/lib/python3.9/asyncio/base_events.py", line 596, in run_forever | |
self._run_once() | |
File "/usr/lib/python3.9/asyncio/base_events.py", line 1854, in _run_once | |
event_list = self._selector.select(timeout) | |
File "/usr/lib/python3.9/selectors.py", line 469, in select | |
fd_event_list = self._selector.poll(timeout, max_ev) | |
KeyboardInterrupt | |
python foo.py 0.10s user 0.00s system 1% cpu 6.820 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment