Python asyncio.streams() Examples

The following are 8 code examples of asyncio.streams(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: patator_task.py    From project-black with GNU General Public License v2.0 6 votes vote down vote up
def start(self):
        await self.all_done.acquire()
        host, port = self.target.split(':')
        args = self.params['program'][0] + " host={} port={}".format(host, port)

        await self.set_status('Working', progress=0)

        socket_name = '{}.sock'.format(self.task_id)
        self.socket_path = os.path.join(os.getcwd(), socket_name)

        if os.path.exists(self.socket_path):
            os.remove(self.socket_path)

        await asyncio.streams.start_unix_server(self.client_connected_cb, path=self.socket_path, loop=self.loop)

        try:
            self.patator_proc = self.loop.run_in_executor(
                ProcessPoolExecutor(), start_program, args,
                host, port, self.task_id, self.project_uuid, 
                self.socket_path
            )
        except Exception as exc:
            print("Exception starting ProcessPoolExecutor", exc) 
Example #2
Source File: client_proto.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def __init__(self, *, loop=None, **kwargs):
        asyncio.streams.FlowControlMixin.__init__(self, loop=loop)
        DataQueue.__init__(self, loop=loop)

        self.paused = False
        self.transport = None
        self.writer = None
        self._should_close = False

        self._message = None
        self._payload = None
        self._payload_parser = None
        self._reading_paused = False

        self._timer = None
        self._skip_status = ()

        self._tail = b''
        self._upgraded = False
        self._parser = None 
Example #3
Source File: client_proto.py    From lambda-text-extractor with Apache License 2.0 6 votes vote down vote up
def __init__(self, *, loop=None, **kwargs):
        asyncio.streams.FlowControlMixin.__init__(self, loop=loop)
        DataQueue.__init__(self, loop=loop)

        self.paused = False
        self.transport = None
        self.writer = None
        self._should_close = False

        self._message = None
        self._payload = None
        self._payload_parser = None
        self._reading_paused = False

        self._timer = None
        self._skip_status = ()

        self._tail = b''
        self._upgraded = False
        self._parser = None 
Example #4
Source File: timing_tcp_server.py    From annotated-py-projects with MIT License 5 votes vote down vote up
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)) 
Example #5
Source File: simple_tcp_server.py    From annotated-py-projects with MIT License 5 votes vote down vote up
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)) 
Example #6
Source File: dirsearch_task.py    From project-black with GNU General Public License v2.0 5 votes vote down vote up
def start(self):
        await self.all_done.acquire()

        await self.set_status('Working', progress=0)

        socket_name = '{}.sock'.format(self.task_id)
        self.socket_path = os.path.join(os.getcwd(), socket_name)

        if os.path.exists(self.socket_path):
            os.remove(self.socket_path)

        server = await asyncio.streams.start_unix_server(self.client_connected_cb, path=self.socket_path, loop=self.loop)

        try:
            self.dirsearch_proc = self.loop.run_in_executor(
                ProcessPoolExecutor(), start_program, self.target,
                self.task_id, self.project_uuid, self.socket_path,
                self.params_object
            )
        except Exception as exc:
            print("Exception starting ProcessPoolExecutor", exc) 
Example #7
Source File: timing_tcp_server.py    From annotated-py-projects with MIT License 4 votes vote down vote up
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() 
Example #8
Source File: simple_tcp_server.py    From annotated-py-projects with MIT License 4 votes vote down vote up
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()