Last active
March 21, 2021 22:07
-
-
Save kangchihlun/742cb86981b14f29a49a197cc322216e to your computer and use it in GitHub Desktop.
asyncio queue demo (can be used in calc trading signal)
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
# asyncq.py | |
import asyncio | |
import itertools as it | |
import os | |
import random | |
import time | |
async def makeitem(size: int = 5) -> str: | |
return os.urandom(size).hex() | |
async def randsleep(a: int = 1, b: int = 5, caller=None) -> None: | |
i = random.randint(0, 10) | |
if caller: | |
print(f"{caller} sleeping for {i} seconds.") | |
await asyncio.sleep(i) | |
async def produce(name: int, q: asyncio.Queue) -> None: | |
n = random.randint(0, 10) | |
for _ in it.repeat(None, n): # Synchronous loop for each single producer | |
await randsleep(caller=f"Producer {name}") | |
i = await makeitem() | |
t = time.perf_counter() | |
await q.put((i, t)) | |
print(f"Producer {name} added <{i}> to queue.") | |
async def consume(name: int, q: asyncio.Queue) -> None: | |
while True: | |
await randsleep(caller=f"Consumer {name}") | |
i, t = await q.get() | |
now = time.perf_counter() | |
print(f"Consumer {name} got element <{i}>" | |
f" in {now-t:0.5f} seconds.") | |
q.task_done() | |
async def main(nprod: int, ncon: int): | |
q = asyncio.Queue() | |
producers = [asyncio.create_task(produce(n, q)) for n in range(nprod)] | |
consumers = [asyncio.create_task(consume(n, q)) for n in range(ncon)] | |
await asyncio.gather(*producers) | |
await q.join() # Implicitly awaits consumers, too | |
for c in consumers: | |
c.cancel() | |
if __name__ == "__main__": | |
import argparse | |
random.seed(444) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--nprod", type=int, default=5) | |
parser.add_argument("-c", "--ncon", type=int, default=10) | |
ns = parser.parse_args() | |
start = time.perf_counter() | |
asyncio.run(main(**ns.__dict__)) | |
elapsed = time.perf_counter() - start | |
print(f"Program completed in {elapsed:0.5f} seconds.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment