v2 的跟帖似乎无法用 markdown 语法? 所以就重新开个帖子请教这个问题。。。
# coding=utf-8
import codecs
import difflib
import os.path
import re
import time
import string
import chardet
import shutil
import copy
from concurrent.futures import ProcessPoolExecutor
import datetime
def PrintCount(PName):
if PName == '甲':
DelayTime = 0.9
if PName == '乙':
DelayTime = 1.2
if PName == '丙':
DelayTime = 1.7
if PName == '丁':
DelayTime = 2
if PName == '戊':
DelayTime = 2.5
countt = 0
while True:
countt += 1
time.sleep(DelayTime)
print(f'{PName}池:->',f'{countt}')
if __name__ == '__main__':
StartTime = time.clock()
FutureDict = {}
FutureRetDict = {}
FutureTimeRecoderDict = {}
PoolNameList = ["甲", "乙", "丙", "丁", "戊"]
# 初始化进程池
for i in range(len(PoolNameList)):
# 进程
FutureDict.update({PoolNameList[i]: ProcessPoolExecutor(max_workers=1)})
# 进程 Ret
FutureRetDict.update({PoolNameList[i]: futures.Future()})
# 进程启动时间
FutureTimeRecoderDict.update({PoolNameList[i]: None})
# 模拟测试清空进程池的信号
closeflag = True
# 开始工作
while True:
ProcessNum = 0
# 增加任务
for ProcessName,FutureRet in FutureRetDict.items():
# 模拟 40 秒后终结 [乙] 进程池
if closeflag == True:
if time.clock() - StartTime >= 40: # 在启动 40 秒后触发
print(f"开始强制结束 [乙] 进程池")
for pid, process in FutureDict['乙']._processes.items():
process.terminate()
FutureDict['乙'].shutdown()
closeflag = False
time.sleep(15)
# 如果进程池在运行
if FutureRet.running() == True:
pass
else:
# 增加任务
FutureRetDict[ProcessName] = FutureDict[ProcessName].submit(PrintCount,ProcessName)
print(f'{ProcessName} 进程池提交了开始.')
time.sleep(2)
break
time.sleep(3)
40 秒之后,乙进程池的确被停了,但是再向乙进程池提交任务的时候,会提示:
甲池:-> 57
甲池:-> 58
丙池:-> 25
戊池:-> 13
甲池:-> 59
丁池:-> 19
甲池:-> 60
丙池:-> 26
Traceback (most recent call last):
File "D:/TestForMu.py", line 80, in <module>
FutureRetDict[ProcessName] = FutureDict[ProcessName].submit(PrintCount,ProcessName)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\concurrent\futures\process.py", line 452, in submit
raise BrokenProcessPool('A child process terminated '
concurrent.futures.process.BrokenProcessPool: A child process terminated abruptly, the process pool is not usable anymore
甲池:-> 61
戊池:-> 14
丁池:-> 20
丙池:-> 27
应该要做些什么事情,再次启用?
原谅我这样设计流程,也许不是科学的。。。^_^
谢谢大家解答!
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.