任何人都可以告诉我为什么以下代码会出现错误“无法启动进程两次”?通过我的计算,p1和p2应该已经被p.terminate()命令强制关闭
编辑:添加了一些代码以提供上下文 - 想要提出一个简单的例子,但遗漏了while循环import time
import os
from multiprocessing import Process
import datetime
def a():
print ("a starting")
time.sleep(30)
print ("a ending")
def b():
print ("b starting")
time.sleep(30)
print ("b ending")
morning = list(range(7,10))
lunch = list(range(11,14))
evening = list(range(17,21))
active = morning + lunch + evening
if __name__=='__main__':
p1 = Process(target = a)
p2 = Process(target = b)
while True:
while (datetime.datetime.now().time().hour) in active:
p1.start()
p2.start()
time.sleep(5)
p1.terminate()
p2.terminate()
time.sleep(5)
else:
print ("Outside hours, waiting 30 mins before retry")
time.sleep(1800)
答案 0 :(得分:2)
它说你不能两次开始一个过程。这正是您在终止后再次呼叫p1.start()
和p2.start()
时所做的事情。尝试重新创建它们就像你在开始时那样。
p1.terminate()
p2.terminate()
time.sleep(5)
p1 = Process(target = a)
p2 = Process(target = b)
p1.start()
p2.start()