giambio/tests/memory_channel.py

32 lines
839 B
Python
Raw Normal View History

2022-02-27 12:41:23 +01:00
import giambio
from debugger import Debugger
async def sender(c: giambio.MemoryChannel, n: int):
for i in range(n):
await c.write(str(i))
print(f"Sent {i}")
await c.close()
print("Sender done")
async def receiver(c: giambio.MemoryChannel):
while True:
if not await c.pending() and c.closed:
print("Receiver done")
break
item = await c.read()
print(f"Received {item}")
await giambio.sleep(1)
async def main(channel: giambio.MemoryChannel, n: int):
print("Starting sender and receiver")
2022-02-27 12:41:23 +01:00
async with giambio.create_pool() as pool:
await pool.spawn(sender, channel, n)
await pool.spawn(receiver, channel)
print("All done!")
2022-02-27 12:41:23 +01:00
giambio.run(main, giambio.MemoryChannel(2), 5, debugger=()) # 2 is the max size of the channel