Created
July 17, 2019 15:16
-
-
Save jiaaro/6952814c6841c88fca52c970a6fab1f0 to your computer and use it in GitHub Desktop.
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 sum_queue_vals(q): | |
result = 0 | |
while True: | |
val = await q.get() | |
if val is None: | |
break | |
result += val | |
return result | |
async def populate_queue(q): | |
for i in range(1000000): | |
if not i % 100000: | |
print(f"put {i} vals in queue so far") | |
await q.put(i) | |
# Send "done" sentinal | |
await q.put(None) | |
loop = asyncio.get_event_loop() | |
data_queue_size = 100 | |
data_q = asyncio.Queue(data_queue_size) | |
loop.create_task(populate_queue(data_q)) | |
result = loop.run_until_complete(sum_queue_vals(data_q)) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment