简体   繁体   中英

How to restart a process using python multiprocessing module

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

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 .

terminate() process will not allow to restart the process but kill() process can be used and the process can be restarted. it works

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.kill()
print "after kill"
time.sleep(3)


p1.start()

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