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

32 lines
869 B
Python

import aiosched
from debugger import Debugger
async def sender(c: aiosched.NetworkChannel, n: int):
for i in range(n):
await c.write(str(i).encode())
print(f"Sent {i}")
await c.close()
print("Sender done")
async def receiver(c: aiosched.NetworkChannel):
while True:
if not await c.pending() and c.closed:
print("Receiver done")
break
item = (await c.read(1)).decode()
print(f"Received {item}")
await aiosched.sleep(1)
async def main(channel: aiosched.NetworkChannel, 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.NetworkChannel(), 5, debugger=()) # 2 is the max size of the channel