Created
August 21, 2020 00:09
-
-
Save ruohola/a8f18d496c9e2760620996e80b5692d7 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 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