This repository has been archived on 2023-05-12. You can view files and clone it, but cannot push or open issues or pull requests.
aiosched/tests/queue.py

42 lines
1.0 KiB
Python

import aiosched
async def producer(q: aiosched.Queue, n: int):
for i in range(n):
# This will wait until the
# queue is emptied by the
# consumer
await q.put(i)
print(f"Produced {i}")
await q.put(None)
print("Producer done")
async def consumer(q: aiosched.Queue):
while True:
# Hangs until there is
# something on the queue
item = await q.get()
if item is None:
print("Consumer done")
break
print(f"Consumed {item}")
# Simulates some work so the
# producer waits before putting
# the next value
await aiosched.sleep(1)
async def main(q: aiosched.Queue, n: int):
print("Starting consumer and producer")
async with aiosched.with_context() as ctx:
await ctx.spawn(producer, q, n)
await ctx.spawn(consumer, q)
print("Bye!")
if __name__ == "__main__":
queue = aiosched.Queue(2) # Queue has size limit of 2
aiosched.run(main, queue, 5, debugger=None)