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

39 lines
1.2 KiB
Python

import aiosched
from debugger import Debugger
async def producer(c: aiosched.NetworkChannel, n: int):
print("[producer] Started")
for i in range(n):
await c.write(str(i).encode())
print(f"[producer] Sent {i}")
await aiosched.sleep(0.5) # This makes the receiver wait on us!
#await c.close()
print("[producer] Done")
async def consumer(c: aiosched.NetworkChannel):
print("[receiver] Started")
try:
while await c.pending():
item = await c.read(1)
print(f"[consumer] Received {item.decode()}")
# await aiosched.sleep(2) # If you uncomment this, the except block will be triggered
except aiosched.errors.ResourceClosed:
print("[consumer] Stream has been closed early!")
print("[consumer] Done")
async def main(channel: aiosched.NetworkChannel, n: int):
t = aiosched.clock()
print("[main] Starting children")
async with aiosched.with_context() as ctx:
await ctx.spawn(consumer, channel)
await ctx.spawn(producer, channel, n)
print(f"[main] All done in {aiosched.clock() - t:.2f} seconds")
aiosched.run(
main, aiosched.NetworkChannel(), 5, debugger=()
) # 2 is the max size of the channel