Created
September 9, 2020 17:27
-
-
Save legnaleurc/2959b59293f8653dc1dd4002aa5648ab to your computer and use it in GitHub Desktop.
producer_consumer.py
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
#! /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