Skip to content

Instantly share code, notes, and snippets.

@tekumara
Created October 4, 2021 08:47
Show Gist options
  • Save tekumara/0ab76fc47b0e760a48175b92a6c502d4 to your computer and use it in GitHub Desktop.
Save tekumara/0ab76fc47b0e760a48175b92a6c502d4 to your computer and use it in GitHub Desktop.
import asyncio
import time
async def heartbeat():
while True:
start = time.time()
await asyncio.sleep(1)
delay = time.time() - start - 1
print(f"heartbeat delay = {delay:.3f}s")
JOB_DURATION = 0.01 # 10ms
async def process():
time.sleep(JOB_DURATION) # simulate CPU time
JOB_COUNT = 200
WORKER_COUNT = 4 # max "active" jobs at a time
async def main_with_queue():
asyncio.create_task(heartbeat())
await asyncio.sleep(2.5)
queue = asyncio.Queue(maxsize=1)
async def worker():
while True:
coro = await queue.get()
await coro # consider using try/except
queue.task_done()
workers = [asyncio.create_task(worker()) for _ in range(WORKER_COUNT)]
print("begin processing")
for _ in range(JOB_COUNT):
await queue.put(process())
await queue.join()
print("end processing")
for w in workers:
w.cancel()
await asyncio.sleep(2)
# asyncio.run(heartbeat())
asyncio.run(main_with_queue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment