async def p1_b(cin): print("Bip 1") alt = Alternative(cin) for _ in range(10): print("ding 1") async with alt as (g, val): print("p1_b: got from select:", g, type(g), val)
async def p1(cin): print("Bip 1") alt = Alternative(cin) for i in range(10): print(f"p1: ding {i}") _, ret = await alt.select() print("p1: got from select:", ret, type(ret))
async def AltTest_p(): sg1 = Skip() sg2 = Skip() ch = Channel('p') alt = Alternative(sg1, sg2, ch.read) ret = await alt.select() print("Returned from alt.select():", ret)
async def alt_timer(cin): print("This is alttimer") for _ in range(10): timer = Timer(0.100) alt = Alternative(cin, timer) (g, ret) = await alt.select() if type(g) is not Timer: print("alttimer: got :{}".format(ret)) else: print("alttimer: timeout")
async def alt_writer(cout): print("This is altwriter") for i in range(10): val = f"sendpkt{i}" print(" -- altwriter sending with alt", val) g = cout.alt_pending_write(val) alt = Alternative(g) ret = await alt.select() print(" -- alt_writer done, got ", ret) print(" ** ch queues : ", cout._chan.rqueue, cout._chan.wqueue)
async def stressed_reader(channels, writers_per_chan): print("Waiting 5 seconds for all writers to get going") await asyncio.sleep(5) print("- sleep done, reader ready") print( f"Setting up alt with {writers_per_chan} procs per channel and {len(channels)} channels." ) print(f"Total writer procs : {writers_per_chan * len(channels)}") alt = Alternative(*[ch.read for ch in channels]) print("Select using async with : ") for run in range(N_RUNS): t1 = time.time() for _ in range(N_SELECTS): async with alt as (_, _): # the selected read operation is already executed, so we have the value already pass t2 = time.time() dt = t2 - t1 us_per_select = 1_000_000 * dt / N_SELECTS print( f"Run {run:2}, {N_SELECTS} iters, {us_per_select} us per select/iter" ) print("Select using alt.select() : ") for run in range(N_RUNS): t1 = time.time() for _ in range(N_SELECTS): await alt.select() # the selected read operation is already executed, so we have the value already t2 = time.time() dt = t2 - t1 us_per_select = 1_000_000 * dt / N_SELECTS print( f"Run {run:2}, {N_SELECTS} iters, {us_per_select} us per select/iter" ) for ch in channels: await ch.poison()
async def Mux2(cin1, cin2, cout): alt = Alternative(cin1, cin2) while True: _, val = await alt.priSelect() await cout(val)