I need to implement something like this:
while True:
conn,addr = socket.accept()
restore()
p.kill()
#...
p = subprocess.Popen(...)
But I need that the restore()
function is called not only after every new connection, but also as soon as p dies.
I can "block" my program waiting for p death by doing p.wait()
, however I also want, at the same time, to block my program waiting for a new connection.
In other words, I need to block my program until one of these two conditions is true : "p dies" OR "new connection".
I know I can use select
to wait for two file descriptors, but I don't know how to do in this case.
Thanks
This is the solution I ended up using:
def wait_socket_and_process(s,process):
while True:
rr,_,_ = select.select([s],[],[],0.1)
if len(rr)>0:
conn, addr = rr[0].accept()
process.kill()
process.poll()
#handle process termination
print "* subprocess killed"
return conn, addr
elif (process.poll() != None):
#handle process termination
print "* subprocess terminated"
s = socket.#...
process = subprocess.Popen(...)
wait_socket_and_process(s,process)
#handle new connection...
As others suggest, there may be cleaner/more general ways to do so using libaries like getenv
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.