python 2.7 中重启时的多处理进程错误
Multiprocessing Process error when restart in python 2.7
您认为以下代码有什么问题?
from multiprocessing import Process as multicore
class tbe_worker(multicore):
def __init__(self):
multicore.__init__(self)
print "init tbe_worker"
def run(self):
print "run tbe_worker"
class Main:
def __init__(self):
self.w_tbe = tbe_worker()
print self.w_tbe
print "create w_tbe instance"
def startelab(self):
print "start thread"
print "alive:", self.w_tbe.is_alive()
print self.w_tbe
self.w_tbe.start()
print "after start"
print self.w_tbe
def stopelab(self):
print "alive:", self.w_tbe.is_alive()
print "exitcode:", self.w_tbe.exitcode
if self.w_tbe.is_alive():
print "alive:", self.w_tbe.is_alive()
self.w_tbe.terminate()
print "alive:", self.w_tbe.is_alive()
self.w_tbe.join()
print "alive:", self.w_tbe.is_alive()
print self.w_tbe
def run(self):
print "before main run"
while True:
x = raw_input()
if x == "v":
self.startelab()
else:
self.stopelab()
print "after main run"
if __name__ == '__main__':
Main().run()
如果我执行以下操作:
- 初始化进程
- 启动进程->(进程立即结束)
- 验证进程是否完成并执行 join ()
- 重启进程
这是测试的输出:
init tbe_worker
<tbe_worker(tbe_worker-1, initial)>
create w_tbe instance
before main run
v
start thread
alive: False
<tbe_worker(tbe_worker-1, initial)>
after start
<tbe_worker(tbe_worker-1, started)>
run tbe_worker
c
alive: False
exitcode: 0
alive: False
<tbe_worker(tbe_worker-1, stopped)>
v
start thread
alive: False
<tbe_worker(tbe_worker-1, stopped)>
我收到这个错误:
File "C:/Program Files/Python27x64/lib/multiprocessing/process.py", line 120,
in start
assert self._popen is None, 'cannot start a process twice'
AssertionError: cannot start a process twice
Press any key to continue . . .
可能是进程完成和终止后不能多次启动?
如果是这样,每次你想开始一份新工作时我都必须创建一个新流程吗? (好像很奇怪)
但最重要的是,它只发生在我身上?因为在网上我找不到关于它的争论。
肯定有一些我遗漏的东西,但我不知道是什么......
来自 multiprocessing 文档。
start()
Start the process’s activity.
This must be called at most once per process object. It arranges for the object’s run() method to be invoked in a separate process.
如果你想再次启动你的目标函数,你需要创建一个新的进程对象。流程对象是唯一的,它们的生命周期与流程本身绑定。
您认为以下代码有什么问题?
from multiprocessing import Process as multicore
class tbe_worker(multicore):
def __init__(self):
multicore.__init__(self)
print "init tbe_worker"
def run(self):
print "run tbe_worker"
class Main:
def __init__(self):
self.w_tbe = tbe_worker()
print self.w_tbe
print "create w_tbe instance"
def startelab(self):
print "start thread"
print "alive:", self.w_tbe.is_alive()
print self.w_tbe
self.w_tbe.start()
print "after start"
print self.w_tbe
def stopelab(self):
print "alive:", self.w_tbe.is_alive()
print "exitcode:", self.w_tbe.exitcode
if self.w_tbe.is_alive():
print "alive:", self.w_tbe.is_alive()
self.w_tbe.terminate()
print "alive:", self.w_tbe.is_alive()
self.w_tbe.join()
print "alive:", self.w_tbe.is_alive()
print self.w_tbe
def run(self):
print "before main run"
while True:
x = raw_input()
if x == "v":
self.startelab()
else:
self.stopelab()
print "after main run"
if __name__ == '__main__':
Main().run()
如果我执行以下操作:
- 初始化进程
- 启动进程->(进程立即结束)
- 验证进程是否完成并执行 join ()
- 重启进程
这是测试的输出:
init tbe_worker
<tbe_worker(tbe_worker-1, initial)>
create w_tbe instance
before main run
v
start thread
alive: False
<tbe_worker(tbe_worker-1, initial)>
after start
<tbe_worker(tbe_worker-1, started)>
run tbe_worker
c
alive: False
exitcode: 0
alive: False
<tbe_worker(tbe_worker-1, stopped)>
v
start thread
alive: False
<tbe_worker(tbe_worker-1, stopped)>
我收到这个错误:
File "C:/Program Files/Python27x64/lib/multiprocessing/process.py", line 120,
in start
assert self._popen is None, 'cannot start a process twice'
AssertionError: cannot start a process twice
Press any key to continue . . .
可能是进程完成和终止后不能多次启动?
如果是这样,每次你想开始一份新工作时我都必须创建一个新流程吗? (好像很奇怪)
但最重要的是,它只发生在我身上?因为在网上我找不到关于它的争论。
肯定有一些我遗漏的东西,但我不知道是什么......
来自 multiprocessing 文档。
start()
Start the process’s activity.
This must be called at most once per process object. It arranges for the object’s run() method to be invoked in a separate process.
如果你想再次启动你的目标函数,你需要创建一个新的进程对象。流程对象是唯一的,它们的生命周期与流程本身绑定。