用 asyncio 做了一个 UDP 传输性能测试工具,目前单进程服务端性能不够,流量大的时候处理不过来,服务端用的 asyncio.DatagramProtocol,怎么变成多进程的呢?试试了一下抢占式的写法,运行报错了,运行起来也只有一个进程工作,上代码
import asyncio
import time
import socket
from multiprocessing import Process
loop = asyncio.get_event_loop()
size = 0
class ServerProtocol(asyncio.DatagramProtocol):
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr, args=None):
global size
data = data.decode()
message = data
index = data.find('\n')
if index > 0:
filename = data[0:index]
data = data[index+1::]
size += len(data)
task = self.WriteToFile(filename, data)
asyncio.run_coroutine_threadsafe(task, loop)
async def WriteToFile(self, f, data):
await asyncio.sleep(1)
return True
async def print_size():
global size
while True:
await asyncio.sleep(1)
print(size)
def start(sock):
listen = loop.create_datagram_endpoint(
ServerProtocol,
sock = sock
)
transport, protocol = loop.run_until_complete(listen)
task = print_size()
asyncio.run_coroutine_threadsafe(task, loop)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
transport.close()
loop.close()
sock.close()
if __name__ == '__main__':
print("Starting UDP server")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 9873))
for i in range(1):
t = Process(target=start, args=(sock,))
t.deamon = True
t.start()
start(sock)
报错信息
Exception in callback BaseSelectorEventLoop._add_reader(6, <bound method..., bufsize=0>>>)
handle: <Handle BaseSelectorEventLoop._add_reader(6, <bound method..., bufsize=0>>>)>
Traceback (most recent call last):
File "/usr/local/python36/lib/python3.6/asyncio/selector_events.py", line 264, in _add_reader
key = self._selector.get_key(fd)
File "/usr/local/python36/lib/python3.6/selectors.py", line 191, in get_key
raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '6 is not registered'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/python36/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/usr/local/python36/lib/python3.6/asyncio/selector_events.py", line 267, in _add_reader
(handle, None))
File "/usr/local/python36/lib/python3.6/selectors.py", line 412, in register
self._epoll.register(key.fd, epoll_events)
FileExistsError: [Errno 17] File exists
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.