简体   繁体   English

如何在asyncio和transport_base类中创建python并行套接字?

[英]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 我在Python中为我的不间断服务器使用了asyncio,并在ServerClientProtocol中实现了connection_made,connection_lost,data_received功能

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()

server.server() might be blocking the other connections. server.server()可能阻止了其他连接。 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) 如果这是一个长时间运行的调用,请尝试使用asyncio.start_server此处为示例),然后使用await loop.run_in_executor(None, server.server, data)调用server.server() await loop.run_in_executor(None, server.server, data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM