Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 622
Jobish Jose Avatar asked Feb 07 '19 14:02

Jobish Jose


People also ask

How do you close a multiprocessing process in Python?

We can kill or terminate a process immediately by using the terminate() method. We will use this method to terminate the child process, which has been created with the help of function, immediately before completing its execution.

What is the default method to start a multiprocessing process in Linux?

The possible start methods are 'fork', 'spawn' and 'forkserver'. On Windows only 'spawn' is available. On Unix 'fork' and 'spawn' are always supported, with 'fork' being the default.

How does Python multiprocess work?

The multiprocessing Python module contains two classes capable of handling tasks. The Process class sends each task to a different processor, and the Pool class sends sets of tasks to different processors.

How does multiprocessing lock work in Python?

Python provides a mutual exclusion lock for use with processes via the multiprocessing. Lock class. An instance of the lock can be created and then acquired by processes before accessing a critical section, and released after the critical section. Only one process can have the lock at any time.


Video Answer


2 Answers

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)
like image 186
Free Code Avatar answered Nov 15 '22 00:11

Free Code


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.

like image 27
Darkonaut Avatar answered Nov 14 '22 22:11

Darkonaut