简体   繁体   中英

Multiprocessing Process error when restart in python 2.7

What do you think is wrong with the following code?

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()  

If I do the following actions:

  • initialize the process
  • start the process -> (the process ends immediately)
  • verify that the process is finished and do the join ()
  • restart the process

This is the output of the test:

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)>

I get this error:

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 . . .

It may be that the process can not start more than once after completion and termination of the same? If so, do I have to create a new process each time you want to start a new job? (It seems strange thing)
But above all, it only happens to me? Because on the web I find no argument about it.
There is definitely something I'm missing, but I can not figure out what ...

From multiprocessing documentation.

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.

If you want to launch again your target function, you need to create a new Process object. Process objects are unique and their lifecycle is bound to the process themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM