Skip to content

Instantly share code, notes, and snippets.

@ruohola
Created August 21, 2020 00:09
Show Gist options
  • Save ruohola/a8f18d496c9e2760620996e80b5692d7 to your computer and use it in GitHub Desktop.
Save ruohola/a8f18d496c9e2760620996e80b5692d7 to your computer and use it in GitHub Desktop.
import asyncio
import random
import time
async def producer(queue):
while True:
await asyncio.sleep(2)
await queue.put(random.randint(1, 100))
async def consumer(queue):
while True:
val = await queue.get()
await asyncio.sleep(2)
print(f"time: {time.strftime('%X')}, consumed: {val}")
async def main():
queue = asyncio.Queue()
await asyncio.gather(producer(queue), consumer(queue))
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment