async def main(): start = giambio.clock() try: async with giambio.create_pool() as pool: # This pool will run until completion of its # tasks and then propagate the exception. This is # because exception in nested pools are propagated # all the way down first, then the pools above the # one that raised the error first wait for their # children to complete and only then re-raise the original exception await pool.spawn(child) await pool.spawn(child) print("[main] First 2 children spawned, awaiting completion") async with giambio.create_pool() as a_pool: await a_pool.spawn(child1) print( "[main] Third children spawned, prepare for trouble in 2 seconds" ) async with giambio.create_pool() as new_pool: # This pool will be cancelled by the exception # in the outer pool await new_pool.spawn(child2) await new_pool.spawn(child3) print("[main] Fourth and fifth children spawned") except Exception as error: # Because exceptions just *work*! print(f"[main] Exception from child caught! {repr(error)}") print( f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds" )
async def main(): start = giambio.clock() async with giambio.create_pool() as pool: await pool.spawn(child, 1) await pool.spawn(child, 2) async with giambio.create_pool() as a_pool: await a_pool.spawn(child, 3) await a_pool.spawn(child, 4) # This executes after spawning all 4 tasks print("[main] Children spawned, awaiting completion") # This will *only* execute when everything inside the async with block # has ran, including any other pool print( f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds" )
async def serve(bind_address: tuple): """ Serves asynchronously forever :param bind_address: The address to bind the server to represented as a tuple (address, port) where address is a string and port is an integer """ sock = giambio.socket.socket() await sock.bind(bind_address) await sock.listen(5) logging.info( f"Serving asynchronously at {bind_address[0]}:{bind_address[1]}") async with giambio.create_pool() as pool: async with sock: while True: try: conn, address_tuple = await sock.accept() logging.info( f"{address_tuple[0]}:{address_tuple[1]} connected") await pool.spawn(handler, conn, address_tuple) except Exception as err: # Because exceptions just *work* logging.info( f"{address_tuple[0]}:{address_tuple[1]} has raised {type(err).__name__}: {err}" )
async def main(): start = giambio.clock() try: async with giambio.create_pool() as pool: await pool.spawn(child) await pool.spawn(child1) print("[main] First 2 children spawned, awaiting completion") async with giambio.create_pool() as new_pool: # This pool will be cancelled by the exception # in the outer pool await new_pool.spawn(child2) await new_pool.spawn(child3) print("[main] Third and fourth children spawned") except Exception as error: # Because exceptions just *work*! print(f"[main] Exception from child caught! {repr(error)}") print(f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds")
async def proxy_two_way(a: giambio.socket.AsyncSocket, b: giambio.socket.AsyncSocket): """ Sets up a two-way proxy from a to b and from b to a """ async with giambio.create_pool() as pool: await pool.spawn(proxy_one_way, a, b) await pool.spawn(proxy_one_way, b, a)
async def main(): start = giambio.clock() async with giambio.create_pool() as pool: await pool.spawn(child) await pool.spawn(child1) print("[main] Children spawned, awaiting completion") print( f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds" )
async def main(): queue = giambio.Queue() lock = giambio.Lock() async with giambio.create_pool() as pool: await pool.spawn(child, 1, 5, queue, lock) await pool.spawn(child, 6, 10, queue, lock) await pool.spawn(other, 10) await pool.spawn(other, 5) print(f"Result: {queue}") # Queue is ordered! print("[main] Done")
async def main(): start = giambio.clock() async with giambio.create_pool() as pool: # await pool.spawn(child, 1) # If you comment this line, the pool will exit immediately! task = await pool.spawn(child, 2) print("[main] Children spawned, awaiting completion") await task.cancel() print("[main] Second child cancelled") print( f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds" )
async def main(): start = giambio.clock() try: async with giambio.create_pool() as pool: await pool.spawn(child) await pool.spawn(child1) print("[main] Children spawned, awaiting completion") except Exception as error: # Because exceptions just *work*! print(f"[main] Exception from child caught! {repr(error)}") print(f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds")
async def parent(pause: int = 1): async with giambio.create_pool() as pool: event = giambio.Event() print("[parent] Spawning child task") await pool.spawn(child, event, pause + 2) start = giambio.clock() print(f"[parent] Sleeping {pause} second(s) before setting the event") await giambio.sleep(pause) await event.trigger(True) print("[parent] Event set, awaiting child completion") end = giambio.clock() - start print(f"[parent] Child exited in {end:.2f} seconds")
async def main(host: Tuple[str, int]): """ Main client entry point """ queue = giambio.Queue() async with giambio.create_pool() as pool: async with giambio.socket.socket() as sock: await sock.connect(host) await pool.spawn(sender, sock, queue) await pool.spawn(receiver, sock, queue) while True: op, data = await queue.get() if op == 0: print(f"Sent.") else: print(f"Received: {data}")
async def main(q: giambio.Queue, n: int): async with giambio.create_pool() as pool: await pool.spawn(producer, q, n) await pool.spawn(consumer, q) print("Bye!")
async def main(channel: giambio.MemoryChannel, n: int): async with giambio.create_pool() as pool: await pool.spawn(sender, channel, n) await pool.spawn(receiver, channel)