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/memory_channel.py

34 lines
822 B
Python

import aiosched
async def sender(c: aiosched.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: aiosched.MemoryChannel):
while True:
if not await c.pending() and c.closed:
print("Receiver done")
break
item = await c.read()
print(f"Received {item}")
await aiosched.sleep(1)
async def main(channel: aiosched.MemoryChannel, n: int):
print("Starting sender and receiver")
async with aiosched.with_context() as ctx:
await ctx.spawn(sender, channel, n)
await ctx.spawn(receiver, channel)
print("All done!")
aiosched.run(
main, aiosched.MemoryChannel(2), 5, debugger=()
) # 2 is the max size of the channel