简体   繁体   English

如何使用python多处理模块重新启动进程

[英]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.我正在尝试使用多处理模块重新启动 python 进程,但出现“断言错误:无法启动进程两次”。

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 .实际上,我正在尝试创建一个进程监视器功能来监视所有进程的内存和 CPU 使用情况。 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.因为在 Unix-y 系统上,父进程需要在内核清除进程表中的相应条目之前读取退出代码。

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.在 Unix 上,当一个进程完成但尚未加入时,它会变成僵尸。 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.永远不应该有很多,因为每次新进程启动(或调用 active_children() )时,所有尚未加入的已完成进程都将被加入。 Also calling a finished process's Process.is_alive will join the process.同时调用已完成进程的 Process.is_alive 将加入该进程。 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.实际上,我正在尝试创建一个进程监视器功能来监视所有进程的内存和 CPU 使用情况。

You should take a look at the psutil module for that.为此,您应该查看psutil模块。

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 .如果你只是想暂停(不是kill)的过程,如果内存消耗得高,你也许可以从我的回答得出一些启示这里

terminate() process will not allow to restart the process but kill() process can be used and the process can be restarted. terminate() 进程将不允许重新启动该进程,但可以使用 kill() 进程并且可以重新启动该进程。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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