How to use the curio.spawn function in curio

To help you get started, we’ve selected a few curio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dabeaz / curio / tests / test_channel.py View on Github external
async def main(ch1, ch2):
        async with ch1, ch2:
            t1 = await spawn(server, ch1)
            t2 = await spawn(client, ch2)
            await t1.join()
            await t2.join()
github povilasb / udptest / udptest / server.py View on Github external
async def _on_data(self, data: bytes, addr: Tuple[str, int]) -> None:
        task = await curio.spawn(self._sock.sendto, b'response', addr)
        await self._tasks.put(task)
github caproto / caproto / tests / test_curio_server.py View on Github external
async def task():
        server_task = await curio.spawn(curio_server)

        try:
            await client()
        finally:
            await server_task.cancel()
github trollfot / trinket / src / trinket / server.py View on Github external
async def serve(self, app: Application):
        Goodbye = curio.SignalEvent(signal.SIGINT, signal.SIGTERM)
        await app.notify('startup')
        task = await curio.spawn(self.run, app)
        await self.ready.set()
        print('Trinket serving on {}:{}'.format(*self.sockaddr))
        await Goodbye.wait()
        print('Server is shutting down.')
        await app.notify('shutdown')
        print('Please wait. The remaining tasks are being terminated.')
        await task.cancel()
        self.ready.clear()
github allenling / curedis / curedis / client.py View on Github external
async def connect(self):
        self.connection = AsyncConnection(self.host, self.port, self.read_size)
        # wait connect done!
        con_task = await curio.spawn(self.connection.connect)
        await con_task.join()
        # send auth command
        if self.password is not None:
            resp = await self.auth(self.password)
            if resp is not True:
                raise RedisAuthError(resp)
        return
github virantha / bricknil / bricknil / bricknil.py View on Github external
task_tcp = await spawn(bricknil_socket_server, web_out_queue, ('',25000))
    await task_tcp.join()

    # Call the user's system routine to instantiate the processes
    await system()

    hub_tasks = []
    hub_peripheral_listen_tasks = [] # Need to cancel these at the end

    # Run the bluetooth listen queue
    task_ble_q = await spawn(ble_q.run())

    # Connect all the hubs first before enabling any of them
    for hub in Hub.hubs:
        hub.web_queue_out = web_out_queue
        task_connect = await spawn(ble_q.connect(hub))
        await task_connect.join()


    for hub in Hub.hubs:
        # Start the peripheral listening loop in each hub
        task_listen = await spawn(hub.peripheral_message_loop())
        hub_peripheral_listen_tasks.append(task_listen)

        # Need to wait here until all the ports are set
        # Use a faster timeout the first time (for speeding up testing)
        first_delay = True
        for name, peripheral in hub.peripherals.items():
            while peripheral.port is None:
                hub.message_info(f"Waiting for peripheral {name} to attach to a port")
                if first_delay:
                    first_delay = False
github Fuyukai / curio_websocket / cuiows / client.py View on Github external
server_hostname = host
        else:
            server_hostname = None

        connection = await curio.open_connection(host=obb.host, port=obb.port, ssl=obb.ssl_context,
                                                 server_hostname=server_hostname)  # type: Socket
        obb.logger.debug("Connection {} opened, sending message to open the websocket"
                         .format(connection))
        obb.connection = connection

        # Write the opening event.
        opening_bytes = obb.ws_state_machine.bytes_to_send()
        await obb.connection.send(opening_bytes)
        obb.logger.debug("Established websocket connection!")

        obb._rd_task = await curio.spawn(obb._reader_task())

        # Wait for the ready event.
        if kwargs.pop("wait_for_ready", True):
            await obb.wait_for_ready()

        return obb
github nokia / moler / examples / layer_1 / curio / network_down_detector.py View on Github external
async def main(host, port):
    # Starting the client
    cli_task = await curio.spawn(ping_observing_task, (host, port))
    await cli_task.join()
github python-hyper / hyper-h2 / examples / curio / curio-server.py View on Github external
async def h2_server(address, root, certfile, keyfile):
    """
    Create an HTTP/2 server at the given address.
    """
    sock = await create_listening_ssl_socket(address, certfile, keyfile)
    print("Now listening on %s:%d" % address)

    async with sock:
        while True:
            client, _ = await sock.accept()
            server = H2Server(client, root)
            await spawn(server.run())
github guyingbo / shadowproxy / legency / shadowproxy_v0_2_5.py View on Github external
async def cmd_associate(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            sock.bind(("", 0))
            host, port = sock.getsockname()
            async with sock:
                await self._stream.write(self._make_resp(host=host, port=port))
                task = await spawn(self.relay_udp(sock))
                while True:
                    data = await self._stream.read()
                    if not data:
                        await task.cancel()
                        return
                    if verbose > 0:
                        print("receive unexpect data:", data)
        except Exception:
            sock._socket.close()