Skip to content

Instantly share code, notes, and snippets.

@jmoz
Created August 23, 2022 05:47
Show Gist options
  • Save jmoz/99e77e35276edd2a2e85d4c9793751f1 to your computer and use it in GitHub Desktop.
Save jmoz/99e77e35276edd2a2e85d4c9793751f1 to your computer and use it in GitHub Desktop.
asyncio producer consumer basic layout
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