簡體   English   中英

在python 2.7中重新啟動時出現Multiprocessing Process錯誤

[英]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 . . .

可能是該過程在完成和終止后不能多次啟動嗎? 如果是這樣,您每次想開始新工作時都必須創建一個新流程嗎? (看起來很奇怪)
但最重要的是,這只發生在我身上嗎? 因為在網絡上我沒有找到任何爭論。
肯定有一些我想念的東西,但我不知道是什么...

來自多處理文檔。

開始()

開始流程的活動。

每個過程對象最多只能調用一次。 它安排在單獨的進程中調用對象的run()方法。

如果要再次啟動目標函數,則需要創建一個新的Process對象。 流程對象是唯一的,它們的生命周期綁定到流程本身。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM