Created
December 5, 2019 16:21
-
-
Save cmrfrd/d4c59a36f12536a5f5c0bda435d0716d to your computer and use it in GitHub Desktop.
async.pysc
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 random | |
| async def p(q,n): | |
| for x in range(n): | |
| print("p: ",x) | |
| await asyncio.sleep(random.random()) | |
| await q.put(str(x)) | |
| async def c(q): | |
| while True: | |
| switch (r := await q.get()): | |
| case "1":print("it's a 1!", r) | |
| case "3":print("it's a 3!", r) | |
| else:print("something!", r) | |
| await asyncio.sleep(random.random()) | |
| q.task_done() | |
| async def run(n): | |
| q = asyncio.Queue() | |
| con = asyncio.ensure_future(c(q)) | |
| await p(q,n) | |
| await q.join() | |
| con.cancel() | |
| loop = asyncio.get_event_loop() | |
| loop.run_until_complete(run(10)) | |
| loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment