如何使用 python 多处理模块重新启动进程
How to restart a process using python multiprocessing module
我正在尝试使用多处理 module 重新启动 python 进程,但是 "AssertionError: cannot start a process twice" 出现了。
我的问题
- 如何重新启动进程
- 一旦终止为什么会变成僵尸mod
- 如何删除僵尸进程
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()
实际上,我正在尝试创建一个进程监视器函数来监视所有进程的内存和 CPU 使用情况。如果达到一定水平我想实时重启
希望对你有所帮助
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?
您不能重新启动已终止的进程。您需要实例化一个新进程。
Once its terminated why it is going to zombie mod?
因为在 Unix-y 系统上,父进程需要在内核从进程中清除相应的条目之前读取退出代码 table。
How can I remove the zombie process?
您有多种选择。我在这里引用 docs:
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.
你应该看看 psutil 模块。
如果您只是想在内存消耗变高时挂起(而不是终止)进程,您或许可以从我的回答中得到一些启发 。
terminate() 进程不允许重新启动进程,但可以使用 kill() 进程并重新启动进程。有效
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()
我正在尝试使用多处理 module 重新启动 python 进程,但是 "AssertionError: cannot start a process twice" 出现了。
我的问题
- 如何重新启动进程
- 一旦终止为什么会变成僵尸mod
- 如何删除僵尸进程
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()
实际上,我正在尝试创建一个进程监视器函数来监视所有进程的内存和 CPU 使用情况。如果达到一定水平我想实时重启
希望对你有所帮助
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?
您不能重新启动已终止的进程。您需要实例化一个新进程。
Once its terminated why it is going to zombie mod?
因为在 Unix-y 系统上,父进程需要在内核从进程中清除相应的条目之前读取退出代码 table。
How can I remove the zombie process?
您有多种选择。我在这里引用 docs:
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.
你应该看看 psutil 模块。
如果您只是想在内存消耗变高时挂起(而不是终止)进程,您或许可以从我的回答中得到一些启发
terminate() 进程不允许重新启动进程,但可以使用 kill() 进程并重新启动进程。有效
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()