How to restart a process using python multiprocessing module
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)
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.