低级版
# async def socket_server():
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# server_add = ('0.0.0.0', 5454)
# s.bind(server_add)
# print(server_add)
# while 1:
# data, addr = s.recvfrom(1024)
# print(data.decode())
# s.close()
#
#
# if __name__ == "__main__":
# asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
# loop = uvloop.new_event_loop()
# asyncio.set_event_loop(loop)
# loop.run_until_complete(socket_server())
低级版不管是用多线程 ,多进程,协程写法,都是单个udp端口,占满阻塞情况
高级版
class MyUDPHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request[0]
print(data)
async def handler():
addr = ('127.0.0.1', 5454)
print("begin")
try:
s = socketserver.ThreadingUDPServer(addr, MyUDPHandler)
s.serve_forever()
except Exception as e:
print(e)
if __name__ == "__main__":
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = uvloop.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(handler())
高级版,启动采用协程方式,udp启动采用多线程udp或fork子进程的形式,经实测,效果预期。