Skip to content

Instantly share code, notes, and snippets.

@legnaleurc
Created September 9, 2020 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save legnaleurc/2959b59293f8653dc1dd4002aa5648ab to your computer and use it in GitHub Desktop.
Save legnaleurc/2959b59293f8653dc1dd4002aa5648ab to your computer and use it in GitHub Desktop.
producer_consumer.py
#! /usr/bin/env python3
import asyncio
from concurrent.futures import ThreadPoolExecutor
import requests
import sys
def background_task():
rv = requests.get('https://httpbin.org/get')
data = rv.json()
return data
async def produce(queue, pool):
loop = asyncio.get_running_loop()
for i in range(5):
t = loop.run_in_executor(pool, background_task)
await queue.put(t)
await asyncio.sleep(1)
await queue.put(None)
async def consume(queue):
while True:
t = await queue.get()
try:
if not t:
break
data = await t
print('consumed', data)
finally:
queue.task_done()
async def main():
q = asyncio.Queue()
with ThreadPoolExecutor() as pool:
await asyncio.wait([produce(q, pool), consume(q)])
await q.join()
return 0
sys.exit(asyncio.run(main()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment