【发布时间】:2015-09-13 07:25:42
【问题描述】:
我需要实现这样的东西:
while True:
conn,addr = socket.accept()
restore()
p.kill()
#...
p = subprocess.Popen(...)
但我需要 restore() 函数不仅在每次新连接后被调用,而且在 p 死亡时也被调用。
我可以通过执行p.wait()“阻止”我的程序等待 p 死亡,但同时我也希望阻止我的程序等待新的连接。
换句话说,我需要阻止我的程序,直到这两个条件之一为真:“p dies”或“new connection”。
我知道我可以使用select 来等待两个文件描述符,但我不知道在这种情况下该怎么做。
谢谢
【问题讨论】:
-
使用 gevent (gevent.org),你的代码和 p.wait() 将按照你想要的方式工作
-
你可以use
signal.set_wakeup_fd()to integratep.wait()intoselect()loop。您可能可以使用asyncio/twisted/gevent而不是自己重新实现功能。
标签: python subprocess python-multiprocessing python-sockets