@
NoAnyLove 好的好的,我查了下,说 IPC 或向 worker 参数传递的东西必须要能 pickle,不然就报错,那就是 select.epoll 是不支持的。error:TypeError: can't pickle select.epoll objects。
代码大致如下:
def listenProcess(serverSocket):
process_num = 2*cpu_count()
# 设置进程池、消息队列
po = Pool(process_num)
q = Manager().Queue() #ipc 方法 1
m=Manager() # ipc 方法 2
epoll_fd = select.epoll()
epoll_fd.register(serverSocket.fileno(), select.EPOLLIN)
# 保存中间数据
connections = {}
addresses = {}
while True:
epoll_list = epoll_fd.poll()
for fd, events in epoll_list:
print("fd is:%s events is%s"%(fd,events))
if fd == serverSocket.fileno():
conn, addr = serverSocket.accept()
conn.setblocking(False)
epoll_fd.register(conn.fileno(), select.EPOLLIN | select.EPOLLET )
connections[conn.fileno()] = conn
addresses[conn.fileno()] = addr
elif events & select.EPOLLIN:
# q.put(connections[fd])
po.apply_async(workProcess4In,args=(connections[fd],epoll_fd,))
elif events & select.EPOLLOUT:
# q.put(fd)
# d=m.dict({"epollfd":epoll_fd,"fd":fd})
po.apply_async(workProcess4Out,(connections[fd,epoll_fd],))
po.close()
po.join()
def workProcess4In(conn,epollfd):
#print("epoll is:%s"%epollfd)
#执行读操作,遇到 eagain 就 modify 状态 out
epollfd.modify(conn.fileno(), select.EPOLLET | select.EPOLLOUT)
执行结果:不执行 workProcess4In,如果把 epoll_fd 换成其他,则没问题
请教 dalao,主进程需要如何才能把 epoll_fd 传过去。