我正在尝试在python Daemon()上下文中使用concurrent.futures ProcessPool执行程序(ThreadPool正在运行),并且每个提交的任务都处于运行状态。
import concurrent.futures
import time
import daemon
import sys
class Daemon():
def __init__(self):
print("init")
self.context = daemon.DaemonContext(
stdout = sys.stdout
)
with self.context:
self.__run()
def task(self, n):
start = time.time()
time.sleep(n)
duration = time.time() - start
return n, duration
def __run(self):
try:
#e = concurrent.futures.ThreadPoolExecutor(3)
e = concurrent.futures.ProcessPoolExecutor(3)
fut = e.submit(self.task, 2)
while not fut.done():
print(fut)
time.sleep(0.5)
res = fut.result()
print(res)
e.shutdown(wait=True)
except Exception as e:
print(e)
finally:
print("stopped")
if __name__ == '__main__':
Daemon()
输出:
# venv/bin/python conc.py
init
<Future at 0x7f85195c8588 state=running>
<Future at 0x7f85195c8588 state=running>
<Future at 0x7f85195c8588 state=running>
[...]
当ThreadPoolExecutor在2秒后正确停止时:
<Future at 0x7f38fd450668 state=running>
<Future at 0x7f38fd450668 state=running>
(30, 2.002039670944214)
stopping
由于并发代码在没有守护进程的情况下运行,我想知道我在这里做错了什么。
我正在使用python 3.5.3和python-daemon 2.1.2