Пример #1
0
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")
Пример #2
0
async def child(ev: giambio.Event, pause: int):
    print("[child] Child is alive! Going to wait until notified")
    start_total = giambio.clock()
    data = await ev.wait()
    end_pause = giambio.clock() - start_total
    print(f"[child] Parent set the event with value {data}, exiting in {pause} seconds")
    start_sleep = giambio.clock()
    await giambio.sleep(pause)
    end_sleep = giambio.clock() - start_sleep
    end_total = giambio.clock() - start_total
    print(
        f"[child] Done! Slept for {end_total:.2f} seconds total ({end_pause:.2f} waiting, {end_sleep:.2f} sleeping), nice nap!"
    )
Пример #3
0
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"
    )
Пример #4
0
    def __init__(self,
                 timeout: float = None,
                 raise_on_timeout: bool = True) -> None:
        """
        Object constructor
        """

        # All the tasks that belong to this pool
        self.tasks: List[giambio.task.Task] = []
        # Whether we have been cancelled or not
        self.cancelled: bool = False
        # The clock time of when we started running, used for
        # timeouts expiration
        self.started: float = giambio.clock()
        # The pool's timeout (in seconds)
        if timeout:
            self.timeout: float = self.started + timeout
        else:
            self.timeout = None
        # Whether our timeout expired or not
        self.timed_out: bool = False
        self._proper_init = False
        self.enclosed_pool: Optional["giambio.context.TaskManager"] = None
        self.raise_on_timeout: bool = raise_on_timeout
        self.entry_point: Optional[giambio.Task] = None
Пример #5
0
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"
    )
Пример #6
0
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"
    )
Пример #7
0
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")
Пример #8
0
async def main():
    start = giambio.clock()
    async with giambio.skip_after(10) as pool:
        await pool.spawn(child, 7)  # This will complete
        await giambio.sleep(2)  # This will make the code below wait 2 seconds
        await pool.spawn(child, 15)  # This will not complete
        await giambio.sleep(50)  # Neither will this
        await child(20)  # Nor this
    if pool.timed_out:
        print("[main] One or more children have timed out!")
    print(
        f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds"
    )
Пример #9
0
async def main():
    start = giambio.clock()
    try:
        async with giambio.with_timeout(12) as pool:
            await pool.spawn(child, 7)  # This will complete
            await giambio.sleep(
                2)  # This will make the code below wait 2 seconds
            await pool.spawn(child, 15)  # This will not complete
            await child(20)  # Neither will this
            await giambio.sleep(50)  # Nor this
    except giambio.exceptions.TooSlowError:
        print("[main] One or more children have timed out!")
    print(
        f"[main] Children execution complete in {giambio.clock() - start:.2f} seconds"
    )
Пример #10
0
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"
    )
Пример #11
0
async def main(delay: int, a: tuple, b: tuple):
    """
    Sets up the proxy
    """

    start = giambio.clock()
    print(
        f"Starting two-way proxy from {a[0]}:{a[1]} to {b[0]}:{b[1]}, lasting for {delay} seconds"
    )
    async with giambio.skip_after(delay) as p:
        sock_a = giambio.socket.socket()
        sock_b = giambio.socket.socket()
        await sock_a.connect(a)
        await sock_b.connect(b)
        async with sock_a, sock_b:
            await proxy_two_way(sock_a, sock_b)
    print(f"Proxy has exited after {giambio.clock() - start:.2f} seconds")
Пример #12
0
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")
Пример #13
0
async def main():
    start = giambio.clock()