是否可以在python多处理中重新启动已经终止的进程?

时间:2016-05-26 13:32:24

标签: python python-multiprocessing

以下是我的问题的示例代码:

import multiprocessing, time
def nopr():
        i=0
        while 1:
                i = i+1
                print i
                time.sleep(1)

p = multiprocessing.Process(target =  nopr)
print "process started"
p.start()
time.sleep(04)
print "process ended"
p.terminate()
time.sleep(1)


p.start()

1 个答案:

答案 0 :(得分:5)

不,你不能启动已终止的进程,你必须重新创建它:

import multiprocessing, time
def nopr():
    i=0
    while 1:
        i = i+1
        print i
        time.sleep(1)

p = multiprocessing.Process(target =  nopr)
print "process started"
p.start()
time.sleep(04)
print "process ended"
p.terminate()
time.sleep(1)

p = multiprocessing.Process(target =  nopr) # recreate
p.start()