Last active
June 23, 2021 05:16
-
-
Save greed2411/2ee4a723c6d67e874ed35525e87b2f30 to your computer and use it in GitHub Desktop.
using asyncio.Queue (from producer & consumer pov) as placeholder to golang-like channels in Python.
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
""" | |
Trying to find a way to handle channels-like situations (producer->consumer) in Python | |
using asyncio.Queue. | |
borrowed code actually from: | |
https://web.archive.org/web/20210507031446/https://asyncio.readthedocs.io/en/latest/producer_consumer.html | |
""" | |
import asyncio | |
import random | |
async def produce(queue: asyncio.Queue): | |
x = 0 | |
while True: | |
# produce an item | |
print('producing {}'.format(x)) | |
# simulate i/o operation using sleep | |
await asyncio.sleep(random.random()) | |
item = str(x) | |
# put the item in the queue | |
await queue.put(item) | |
x += 1 | |
async def consume(queue: asyncio.Queue): | |
while True: | |
# wait for an item from the producer | |
item = await queue.get() | |
# process the item | |
print('consuming item {}...'.format(item)) | |
# simulate i/o operation using sleep | |
await asyncio.sleep(random.random()) | |
async def main(): | |
queue = asyncio.Queue() | |
producer_coro = produce(queue) | |
consumer_coro = consume(queue) | |
await asyncio.gather(producer_coro, consumer_coro) # can't avoid this | |
if __name__ == "__main__": | |
asyncio.run(main()) | |
# # Output: | |
# λ python main.py | |
# producing 0 | |
# producing 1 | |
# consuming item 0... | |
# producing 2 | |
# consuming item 1... | |
# producing 3 | |
# producing 4 | |
# consuming item 2... | |
# producing 5 | |
# consuming item 3... | |
# consuming item 4... | |
# producing 6 | |
# consuming item 5... | |
# producing 7 | |
# producing 8 | |
# consuming item 6... | |
# consuming item 7... | |
# producing 9 | |
# consuming item 8... | |
# producing 10 | |
# consuming item 9... | |
# producing 11 | |
# consuming item 10... | |
# producing 12 | |
# consuming item 11... | |
# producing 13 | |
# consuming item 12... | |
# producing 14 | |
# producing 15 | |
# producing 16 | |
# consuming item 13... | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment