Skip to content

Instantly share code, notes, and snippets.

@cmrfrd
Created December 5, 2019 16:21
Show Gist options
  • Select an option

  • Save cmrfrd/d4c59a36f12536a5f5c0bda435d0716d to your computer and use it in GitHub Desktop.

Select an option

Save cmrfrd/d4c59a36f12536a5f5c0bda435d0716d to your computer and use it in GitHub Desktop.
async.pysc
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