Created
August 23, 2022 05:47
-
-
Save jmoz/99e77e35276edd2a2e85d4c9793751f1 to your computer and use it in GitHub Desktop.
asyncio producer consumer basic layout
This file contains 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 | |
async def producer(queue): | |
i = 1 | |
while True: | |
await queue.put(f'item {i}') | |
i += 1 | |
await asyncio.sleep(5) | |
async def consumer(queue): | |
while True: | |
item = await queue.get() | |
print(item) | |
if __name__ == '__main__': | |
queue = asyncio.Queue() | |
loop = asyncio.get_event_loop() | |
loop.create_task(producer(queue)) | |
loop.create_task(consumer(queue)) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment