Esempio n. 1
0
    async def async_recv() -> Any:
        """Receive data from the process connection asynchronously."""
        while True:
            if not prc.is_alive() or parent_conn.poll():
                break
            await asyncio.sleep(0.5)

        if not prc.is_alive():
            raise ReceiveError
        try:
            return await server.add_executor_job(parent_conn.recv)
        except EOFError as exc:
            LOGGER.debug("Nothing more to receive")
            raise ReceiveError from exc
Esempio n. 2
0
def func_wrapper(create_callback: Callable, conn: Connection,
                 *args: Any) -> None:
    """Wrap a function with connection to receive and send data."""
    running = True

    # pylint: disable=unused-argument
    def handle_signal(signum: int, frame: Any) -> None:
        """Handle signal."""
        nonlocal running
        running = False
        conn.close()

    signal.signal(signal.SIGTERM, handle_signal)
    signal.signal(signal.SIGINT, handle_signal)

    try:
        callback = create_callback(*args)
    except Exception as exc:  # pylint: disable=broad-except
        LOGGER.error("Failed to create callback: %s", exc)
        return

    while running:

        while running:
            if conn.poll():
                break
            sleep(0.5)

        try:
            data = conn.recv()
        except EOFError:
            LOGGER.debug("Nothing more to receive")
            break
        except OSError:
            LOGGER.debug("Connection is closed")
            break
        try:
            result = callback(data)
        except Exception as exc:  # pylint: disable=broad-except
            LOGGER.error("Failed to run callback: %s", exc)
            break

        if not running:
            break
        try:
            conn.send(result)
        except ValueError:
            LOGGER.error("Failed to send result %s", result)
        except OSError:
            LOGGER.debug("Connection is closed")
            break

    LOGGER.debug("Exiting process")
Esempio n. 3
0
async def tcp_client(message: str,
                     host: str = "127.0.0.1",
                     port: int = 8555) -> None:
    """Connect to server and send message."""
    reader, writer = await asyncio.open_connection(host, port)
    data = await reader.readline()
    version_msg = data.decode()
    LOGGER.debug("Version message: %s", version_msg.strip())

    LOGGER.info("Send: %r", message)
    writer.write(message.encode())
    await writer.drain()

    data = await reader.readline()
    LOGGER.info("Received: %r", data.decode())

    LOGGER.debug("Closing the connection")
    writer.close()
    await writer.wait_closed()