Search code examples
pythonparallel-processingpython-asyncioparallelstransport

how to create python parallel sockets in asyncio and transport_base class?


I used asyncio for my non-stop server in python and implemented connection_made , connection_lost , data_received funtions in my ServerClientProtocol

I used this class first beacause of using multiple times repeatedly sending data to socket class socket got closed and program exited

and second becuase I thought its async and have parallel answering multiple coming sockets in same time, but it's not.

how should I use that in one async thread and parallel answering socket?

this is my code:

    class ServerClientProtocol(asyncio.Protocol):    
        def connection_made(self,transport):
            self.transport = transport

        def connection_lost(self,exc):
            pass

        def data_received(self, data):
            server.server(self,data)

    def main(*args):    
        loop = get_event_loop()        
        coro = loop.create_server(ServerClientProtocol, '127.0.0.1', 50008)    
        srv = loop.run_until_complete(coro)    
        loop.run_forever()    
    if __name__ == '__main__':
        main()

Solution

  • server.server() might be blocking the other connections. If this is a long-running call, try using asyncio.start_server (example here) instead, and call server.server() using await loop.run_in_executor(None, server.server, data)