如何在 python 中同时监听命名管道和套接字

How to listen named pipe and socket at the same time in python

我需要同时监听带有 xmpp 连接的套接字以获取传入消息和命名管道 (debian linux)。

在我看来,主要的问题是

之后一切都冻结了
os.open('/var/mypipes/outgoing', os.O_RDONLY)

等待管道,因此,变体如

list = {socket.here:'xmpp',os.open('/var/mypipes/outgoing', os.O_RDONLY):'mypipe'}
while online:
   (i, o, e) = select.select(list.keys(),[],[],1)
   for key in i:
       do smth

不会工作,即使我将那个东西放入 select.select:

while online:
   (i, o, e) = select.select([socket.here,os.open('/var/mypipes/outgoing', os.O_RDONLY)],[],[],1)
   for key in i:
       do smth

它也不起作用。 如您所见,我在 python 方面不是很专业,所以如果您能告诉我在哪里寻找解决方案,那就足够了。 Buuut ...现成的解决方案也不错。 =)

好的,O_NONBLOCK解决了阻塞的问题,select之后可以正常工作,下面的代码在我的系统上运行:

import os
import select
l = {os.open('/tmp/pipe', os.O_RDONLY|os.O_NONBLOCK):'mypipe'}

while True:
    (i, o, e) = select.select(l.keys(),[],[],1)
    for key in i:
        print os.read(key, 1)

您所要做的就是在管道关闭时忽略错误...