How to restart a process using python multiprocessing module

10,435

Solution 1

How can I restart the process?

You cannot restart a terminated process. You need to instantiate a new process.

Once its terminated why it is going to zombie mod?

Because on Unix-y systems the parent process needs to read the exit-code before the kernel clears the corresponding entry from the process table.

How can I remove the zombie process?

You have multiple options. I'm citing the docs here:

Joining zombie processes

On Unix when a process finishes but has not been joined it becomes a zombie. There should never be very many because each time a new process starts (or active_children() is called) all completed processes which have not yet been joined will be joined. Also calling a finished process’s Process.is_alive will join the process. Even so it is probably good practice to explicitly join all the processes that you start.


Actually I am trying to create a process monitor function to watch the memory and CPU usage of all processes.

You should take a look at the psutil module for that.

In case you just want to suspend (not kill) processes if memory consumption gets to high, you might be able to draw some inspiration from my answer here.

Solution 2

I hope it will help you

import time
from multiprocessing import Process

def worker ():
    while True:
        print "Inside the worker"
        time.sleep(10)

def proc_start():
    p_to_start = Process(target=worker,name="worker")
    p_to_start.start()
    return p_to_start


def proc_stop(p_to_stop):
    p_to_stop.terminate()
    print "after Termination "


p = proc_start()
time.sleep(3)
proc_stop(p)
time.sleep(3)

p = proc_start()
print "start gain"
time.sleep(3)
proc_stop(p)
Share:
10,435
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin over 1 year

    I am trying to restart a python process using multiprocessing module, but "AssertionError: cannot start a process twice" appears.

    My question

    • How can I restart the process
    • Once its terminated why it is going to zombie mod
    • How can I remove the zombie process
    import time
    from multiprocessing import Process
    
    def worker ():
        while True:
            print "Inside the worker"
            time.sleep(10)
    
    
    p1 = Process(target=worker,name="worker")
    p1.start()
    #p1.join()
    
    time.sleep(3)
    
    p1.terminate()
    print "after Termination "
    time.sleep(3)
    
    
    p1.start()
    
    
    

    Actually I am trying to create a process monitor function to watch the memory and CPU usage of all processes . If it reach a certain level I want to restart on realtime

  • Mark Zhang
    Mark Zhang about 3 years
    the code will raise an error assert self._popen is None, 'cannot start a process twice'