项目中 继承asyncore中的dispatcher 和 dispatcher_with_send 两个类实现AF_UNIX socket服务器。但是使用top命令查看该服务进程时,发现cpu使用率基本是100%。
经过查找发现,是asyncore库存在bug(使用AF_UNIX sockets时,存在 polling loop ),需要分别修改asyncore.py文件中的 poll和poll2函数的一个if 语句。
1. 修改poll 函数
def poll(timeout=0.0, map=None):
is_w = obj.writable()
if is_r:
r.append(fd)
<span style="color:#FF0000;">#if is_w:</span>
# accepting sockets should not be writable
<span style="color:#009900;">if is_w and not obj.accepting:</span>
w.append(fd)
if is_r or is_w:
e.append(fd)
2. 修改poll2 函数
def poll2(timeout=0.0, map=None):
flags = 0
if obj.readable():
flags |= select.<span style="background-color: rgb(255, 255, 255);">POLLIN</span> | select.POLLPRI
<span style="color:#FF0000;">#if obj.writable():</span>
# accepting sockets should not be writable
<span style="background-color: rgb(0, 153, 0);">if obj.writable() and not obj.accepting:</span>
flags |= select.POLLOUT
if flags:
# Only check for exceptions if object was either readable
https://hg.python.org/cpython/rev/16bc59d3786