我有一个简单的服务器:
from multiprocessing import Pool, TimeoutError
import time
import os
if __name__ == '__main__':
# start worker processes
pool = Pool(processes=1)
while True:
# evaluate "os.getpid()" asynchronously
res = pool.apply_async(os.getpid, ()) # runs in *only* one process
try:
print(res.get(timeout=1)) # prints the PID of that process
except TimeoutError:
print('worker timed out')
time.sleep(5)
pool.close()
print("Now the pool is closed and no longer available")
pool.join()
print("Done")
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,我会得到类似的东西:
47292
47292
Run Code Online (Sandbox Code Playgroud)
然后我kill 47292在服务器运行时.启动了新的工作进程,但服务器的输出是:
47292
47292
worker timed out
worker timed out
worker …Run Code Online (Sandbox Code Playgroud)