2023-05-18 09:55:10 +02:00
|
|
|
import time
|
2023-05-15 18:25:02 +02:00
|
|
|
import structio
|
|
|
|
|
|
|
|
|
|
|
|
async def producer(q: structio.Queue, n: int):
|
|
|
|
for i in range(n):
|
|
|
|
# This will wait until the
|
|
|
|
# queue is emptied by the
|
|
|
|
# consumer
|
|
|
|
await q.put(i)
|
2023-05-18 11:23:34 +02:00
|
|
|
print(f"[producer] Produced {i}")
|
2023-05-15 18:25:02 +02:00
|
|
|
await q.put(None)
|
2023-05-18 11:23:34 +02:00
|
|
|
print("[producer] Producer done")
|
2023-05-15 18:25:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def consumer(q: structio.Queue):
|
|
|
|
while True:
|
|
|
|
# Hangs until there is
|
|
|
|
# something on the queue
|
|
|
|
item = await q.get()
|
|
|
|
if item is None:
|
2023-05-18 11:23:34 +02:00
|
|
|
print("[consumer] Consumer done")
|
2023-05-15 18:25:02 +02:00
|
|
|
break
|
2023-05-18 11:23:34 +02:00
|
|
|
print(f"[consumer] Consumed {item}")
|
2023-05-15 18:25:02 +02:00
|
|
|
# Simulates some work so the
|
|
|
|
# producer waits before putting
|
|
|
|
# the next value
|
|
|
|
await structio.sleep(1)
|
|
|
|
|
|
|
|
|
2023-05-18 09:55:10 +02:00
|
|
|
def threaded_consumer(q: structio.thread.AsyncThreadQueue):
|
|
|
|
while True:
|
|
|
|
# Hangs until there is
|
|
|
|
# something on the queue
|
|
|
|
item = q.get_sync()
|
|
|
|
if item is None:
|
2023-05-18 11:23:34 +02:00
|
|
|
print("[worker consumer] Consumer done")
|
2023-05-18 09:55:10 +02:00
|
|
|
break
|
2023-05-18 11:23:34 +02:00
|
|
|
print(f"[worker consumer] Consumed {item}")
|
2023-05-18 09:55:10 +02:00
|
|
|
# Simulates some work so the
|
|
|
|
# producer waits before putting
|
|
|
|
# the next value
|
|
|
|
time.sleep(1)
|
2023-05-18 11:23:34 +02:00
|
|
|
return 69
|
2023-05-18 09:55:10 +02:00
|
|
|
|
|
|
|
|
2023-05-15 18:25:02 +02:00
|
|
|
async def main(q: structio.Queue, n: int):
|
2023-05-18 11:23:34 +02:00
|
|
|
print("[main] Starting consumer and producer")
|
|
|
|
t = structio.clock()
|
2023-05-15 18:25:02 +02:00
|
|
|
async with structio.create_pool() as ctx:
|
|
|
|
ctx.spawn(producer, q, n)
|
|
|
|
ctx.spawn(consumer, q)
|
2023-05-18 11:23:34 +02:00
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
def threaded_producer(q: structio.thread.AsyncThreadQueue, n: int):
|
|
|
|
print("[worker producer] Producer started")
|
|
|
|
for i in range(n):
|
|
|
|
# This will wait until the
|
|
|
|
# queue is emptied by the
|
|
|
|
# consumer
|
|
|
|
q.put_sync(i)
|
|
|
|
print(f"[worker producer] Produced {i}")
|
|
|
|
q.put_sync(None)
|
|
|
|
print("[worker producer] Producer done")
|
|
|
|
return 42
|
2023-05-15 18:25:02 +02:00
|
|
|
|
|
|
|
|
2023-05-18 09:55:10 +02:00
|
|
|
async def main_threaded(q: structio.thread.AsyncThreadQueue, n: int):
|
2023-05-18 11:23:34 +02:00
|
|
|
print("[main] Starting consumer and producer")
|
|
|
|
t = structio.clock()
|
2023-05-18 09:55:10 +02:00
|
|
|
async with structio.create_pool() as pool:
|
|
|
|
pool.spawn(producer, q, n)
|
2023-05-18 11:23:34 +02:00
|
|
|
val = await structio.thread.run_in_worker(threaded_consumer, q)
|
|
|
|
print(f"[main] Thread returned {val!r}")
|
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
|
|
|
|
|
|
|
|
|
|
|
async def main_threaded_2(q: structio.thread.AsyncThreadQueue, n: int):
|
|
|
|
print("[main] Starting consumer and producer")
|
|
|
|
t = structio.clock()
|
|
|
|
async with structio.create_pool() as pool:
|
|
|
|
pool.spawn(consumer, q)
|
|
|
|
val = await structio.thread.run_in_worker(threaded_producer, q, n)
|
|
|
|
print(f"[main] Thread returned {val!r}")
|
|
|
|
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
|
2023-05-18 09:55:10 +02:00
|
|
|
|
|
|
|
|
2023-05-15 18:25:02 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
queue = structio.Queue(2) # Queue has size limit of 2
|
|
|
|
structio.run(main, queue, 5)
|
2023-05-18 09:55:10 +02:00
|
|
|
queue = structio.thread.AsyncThreadQueue(2)
|
|
|
|
structio.run(main_threaded, queue, 5)
|
2023-05-18 11:23:34 +02:00
|
|
|
structio.run(main_threaded_2, queue, 5)
|