[英]Python Multiprocessing start a process during a for loop
我有一个简单的 for 循环扫描文档,我希望它在继续执行主 for 循环的同时启动第二个进程
def main():
combatDelta = datetime.timedelta(0, 5, 0 , 0)
combatlog = []
othercombats = []
times = []
with open(path, "r") as file:
firstCombat = True
counter = 0
combatstart = None
lastTime = None
betweencombatDetla = None
start = timer.time()
for line in file:
temp2 = line
temp2 = temp2.split("::")
temp = temp2[0]
temp = temp.split(":")
time = datetime.datetime(2000 + int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3]), int(temp[4]),
int(temp[5][0] + temp[5][1]), int(temp[5][3]) * 100000)
if counter == 0:
combatstart = time
lastTime = time
else:
betweencombatDetla = time - lastTime
lastTime = time
if betweencombatDetla > combatDelta:
print("next combat")
if firstCombat:
p = Process(target=combatAnalyis())
p.start()
firstCombat = False
combattime = time - combatstart
if firstCombat:
combatlog.append(line)
else:
othercombats.append(line)
print(len(combatlog))
print(len(othercombats))
counter += 1
(对不起,如果代码有点乱)(目的是,在继续 for 循环执行“combatAnalysis function”的同时,现在这个 function 只是一个 for 循环打印数字作为占位符,以表明多处理是工作)按照我尝试实现的文档进行,但它现在首先执行第二个 function 并且同时不继续 for 循环
尝试使用线程库运行线程。 使用threading.Thread(target=combatAnalyis)
然后使用 .start .start()
来启动它。 确保不要在目标 function 的名称后添加括号-()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.