Python asyncio 模块,streams() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用asyncio.streams()

项目:halite-starter-python3-alt    作者:dvdotsenko    | 项目源码 | 文件源码
def get_stdout(loop, exit_callbacks):
    """

    :param loop:
    :param exit_callbacks: A list of async callables to run before loop.close()
    :return:
    :rtype: asyncio.StreamWriter
    """

    stdout_fio = os.fdopen(os.dup(sys.stdout.fileno()), 'wb')
    writer_transport, writer_protocol = await loop.connect_write_pipe(
        asyncio.streams.FlowControlMixin,
        stdout_fio
    )

    # async def _close_fio():
    #     stdout_fio.close()
    #
    # exit_callbacks.append(
    #     _close_fio
    # )

    # async def _close_transport():
    #     writer_transport.close()
    #
    # exit_callbacks.append(
    #     _close_transport
    # )

    stdout = asyncio.StreamWriter(writer_transport, writer_protocol, None, loop)
    return stdout
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def start(self, loop):
        """
        Starts the TCP server, so that it listens on port 1234.

        For each client that connects, the accept_client method gets
        called.  This method runs the loop until the server sockets
        are ready to accept connections.
        """
        self.server = loop.run_until_complete(
            asyncio.streams.start_server(self._accept_client,
                                         '127.0.0.1', 12345,
                                         loop=loop))
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def start(self, loop):
        """
        Starts the TCP server, so that it listens on port 12345.

        For each client that connects, the accept_client method gets
        called.  This method runs the loop until the server sockets
        are ready to accept connections.
        """
        self.server = loop.run_until_complete(
            asyncio.streams.start_server(self._accept_client,
                                         '127.0.0.1', 12345,
                                         loop=loop))
项目:utils    作者:Ctrlsman    | 项目源码 | 文件源码
def start(self, loop):
        self.server = loop.run_until_complete(
            asyncio.streams.start_server(self._accept_client,
                                         '127.0.0.1', 12345,
                                         loop=loop))
项目:utils    作者:Ctrlsman    | 项目源码 | 文件源码
def main():
    loop = asyncio.get_event_loop()

    server = MyServer()
    server.start(loop)

    @asyncio.coroutine
    def client():
        reader, writer = yield from asyncio.streams.open_connection('127.0.0.1', 12345, loop=loop)

        def send(msg):
            print('>>>:', msg)
            writer.write((msg + '\n').encode('utf-8'))

        def recv():
            msgback = (yield from reader.readline()).decode("utf-8").rstrip()
            print('<<<:', msgback)
            return msgback

        send("add 1 2")
        msg = yield from recv()

        send("repeat 5 hello")
        msg = yield from recv()
        assert msg == 'begin'
        while True:
            msg = yield from recv()
            if msg == 'end':
                break

        writer.close()
        yield from asyncio.sleep(0.5)

        # creates a client and connects to our server

    try:
        loop.run_until_complete(client())
        server.stop(loop)
    finally:
        loop.close()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def main():
    loop = asyncio.get_event_loop()

    # creates a server and starts listening to TCP connections
    server = MyServer()
    server.start(loop)

    @asyncio.coroutine
    def client():
        reader, writer = yield from asyncio.streams.open_connection(
            '127.0.0.1', 12345, loop=loop)

        def send(msg):
            print("> " + msg)
            writer.write((msg + '\n').encode("utf-8"))

        def recv():
            msgback = (yield from reader.readline()).decode("utf-8").rstrip()
            print("< " + msgback)
            return msgback

        # send a line
        send("add 1 2")
        msg = yield from recv()

        Ns = list(range(100, 100000, 10000))
        times = []

        for N in Ns:
            t0 = time.time()
            send("repeat {} hello world ".format(N))
            msg = yield from recv()
            assert msg == 'begin'
            while True:
                msg = (yield from reader.readline()).decode("utf-8").rstrip()
                if msg == 'end':
                    break
            t1 = time.time()
            dt = t1 - t0
            print("Time taken: {:.3f} seconds ({:.6f} per repetition)"
                  .format(dt, dt/N))
            times.append(dt)

        writer.close()
        yield from asyncio.sleep(0.5)

    # creates a client and connects to our server
    try:
        loop.run_until_complete(client())
        server.stop(loop)
    finally:
        loop.close()
项目:annotated-py-asyncio    作者:hhstore    | 项目源码 | 文件源码
def main():
    loop = asyncio.get_event_loop()

    # creates a server and starts listening to TCP connections
    server = MyServer()
    server.start(loop)

    @asyncio.coroutine
    def client():
        reader, writer = yield from asyncio.streams.open_connection(
            '127.0.0.1', 12345, loop=loop)

        def send(msg):
            print("> " + msg)
            writer.write((msg + '\n').encode("utf-8"))

        def recv():
            msgback = (yield from reader.readline()).decode("utf-8").rstrip()
            print("< " + msgback)
            return msgback

        # send a line
        send("add 1 2")
        msg = yield from recv()

        send("repeat 5 hello")
        msg = yield from recv()
        assert msg == 'begin'
        while True:
            msg = yield from recv()
            if msg == 'end':
                break

        writer.close()
        yield from asyncio.sleep(0.5)

    # creates a client and connects to our server
    try:
        loop.run_until_complete(client())
        server.stop(loop)
    finally:
        loop.close()