def
Subpro
(
n
)
:
for
i
in
range
(
n
)
:
print
(
multiprocessing
.
current_process
.
__name__
,
'is going to sleep'
,
i
,
's.'
)
time
.
sleep
(
i
)
def
_get_status
(
subprocess
=
None
)
:
dict = {
'initlal': {
subprocess._popen: None,
subprocess.is_alive(): False,
'started': {
subprocess._popen: is not None,
subprocess.is_alive(): True,
subprocess._popen.poll: is not None
'stopped': {
subprocess._popen: is not None,
subprocess.is_alive(): False,
subprocess._popen.poll: 0 # if exit as expected
'closed': {
subprocess._closed: True
if
subprocess
.
_closed
:
return
'closed'
if
subprocess
.
_popen
is
None
:
if
not
subprocess
.
is_alive
(
)
:
return
'initial'
else
:
exitcode
=
subprocess
.
_popen
.
poll
(
)
if
exitcode
is
not
None
:
exitcode
=
multiprocessing
.
process
.
_exitcode_to_name
.
get
(
exitcode
,
exitcode
)
return
'stopped'
else
:
if
subprocess
.
_parent_pid
!=
os
.
getpid
(
)
:
return
'unknown'
else
:
return
'started'
if
__name__
==
'__main__'
:
pro
=
multiprocessing
.
Process
(
target
=
Subpro
,
args
=
(
3
,
)
,
name
=
'ChildProcess'
)
print
(
'initial'
,
_get_status
(
subprocess
=
pro
)
)
pro
.
start
(
)
print
(
'start'
,
_get_status
(
subprocess
=
pro
)
)
pro
.
join
(
)
print
(
'join'
,
_get_status
(
subprocess
=
pro
)
)
pro
.
close
(
)
print
(
'close'
,
_get_status
(
subprocess
=
pro
)
)
/
Applications
/
Multiprocessing
/
Status
.
py
initial initial
start started
current_process
is
going to sleep
0
s
.
current_process
is
going to sleep
1
s
.
current_process
is
going to sleep
2
s
.
join stopped
close closed
Process finished
with
exit code
0
import threading
import time, os
def Subthread(n):
for i in range(n):
print('is going to sleep', i, 's.')
time.sleep(i)
def _get_status(thread=None):
dict = {
'initlal': {
thread.is_alive(): False,
thread._ident: None,# type <class 'NoneType'>
thread._started.is_set(): False,
thread._is_stopped: False
'started': {
thread.is_alive(): True,
thread._ident: is not None,
thread._started.is_set(): True,
thread._is_stopped: False
'stopped': {
thread.is_alive(): False,
thread._ident: is not None,
thread._started.is_set(): True,
thread._is_stopped: True
if not thread.is_alive():
if thread._is_stopped:
return 'stopped'
else:
return 'initial'
else:
if thread._started.is_set():
return 'started'
if __name__=='__main__':
subthread = threading.Thread(target=Subthread, args=(3, ), name='Childthread')
print('initial', _get_status(thread=subthread))
subthread.start()
print('start', _get_status(thread=subthread))
subthread.join()
print('join', _get_status(thread=subthread))
/Applications/Multithreading/Status.py
initial initial
is going to sleep 0 s.
start started
is going to sleep 1 s.
is going to sleep 2 s.
join stopped
Process finished with exit code 0
写本文时有参考以下链接:
multi-processing-threading/Multiprocessing/Status.py
multi-processing-threading/Multithreading/Status.py
cpython/Lib/multiprocessing/
multiprocessing — Process-based parallelism
cpython/Lib/threading.py
threading — Thread-based parallelism
how to extract the status of a subprocess
在使用selenium库爬取一些小东西,经常出现程序进入死循环的情况。导致一个简简单单的需求,居然花了3天没跑完,很是气愤。遂给写了一个监听程序,当爬虫死机一定时间的时候,就把这只爬虫拍死,然后根据日志从上次爬的地方重启一只新爬虫。当涉及到与操作系统进行交互的时候,Python中的 psutil 和 subprocess 库都是非常有用的工具。
在计算机系统中,进程管理是一个重要的任务,它涉及创建、启动、监控、终止和管理运行中的进程。Python作为一门强大的编程语言,提供了丰富的库和模块,使得进程管理变得相对容易。本文将介绍如何使用Python来实现系统进程管理,包括创建和管理进程、与进程通信以及监控进程的状态。
这里写自定义目录标题一、 什么是 supervisor了?二、Supervisor配置三、启动Supervisor(yum方式安装的)四、实现通知报警功能分析解决第一阶段第二阶段第三阶段
参考:https://blog.51cto.com/u_11110720/2544387
服务器上的应用程序有时候会莫名其妙地挂掉,如果我们经常去登录服务器看是不是程序挂了,挂了再拉起,那样是非常耗时和麻烦的事情。后来我们通过使用 supervisor 去守护启动,实现方法如下。
一、 什么是 supervisor了?
Python中的多线程多线程一个进程中有多个线程就是多线程。一个进程中至少有一个线程,并作为程序的入口,这个就是主线程。一个进程至少有一个主进程,其他线程称为工作线程。线程安全:线程执行一段代码,不会产生不确定的结果,那这段代码就是线程安全。(例如print()线程不安全)线程的daemon属性daemon属性:表示线程是否是daemon线程,这个值必须在start()之前设...
进程:一个程序运行起来后,代码+用到的资源 称之为进程,它是操作系统分配资源的基本单元。
2. 进程的状态
工作中,任务数往往大于cpu的核数,即一定有一些任务正在执行,而另外一些任务在等待cpu进行执行,因此导致了有了不同的状态
就绪态:运行的条件都已经慢去,正在等在cpu执行执行态:cpu正在执行其功能
python multiprocessing 如何在主进程中捕获子进程抛出的异常,适用于子进程无返回值的场景。
推荐使用基于 multiprocessing.Queue() 的方案。
方案 1:
基于 multiprocessing.Queue() 实现子进程和主进程之间报错信息的通信,如果子进程抛出异常,就在 error_queue 队列中 put(error_flag=1) 标志位,否则...
因为hadoop集群中datanode是大量存在的,那么多机器,什么事都可能发生,最通常的大概就是进程挂掉了。所以为了省事,参考别人的代码写了这个监控进程的daemon。当然,稍加修改就可以用来监控别的必须常驻的进程。只需start,不用后面跟&或者前面加nohup。其实很多人都对进程挂掉很头疼,没事半夜得爬起来上服务器启动进程是一件非常痛苦的事情。每2秒监测一次进程,发现进程消失就重启进...
进程池的使用有四种方式:apply_async、apply、map_async、map。其中apply_async和map_async是异步的,也就是启动进程函数之后会继续执行后续的代码不用等待进程函数返回。apply_async和map_async方式提供了一写获取进程函数状态的函数:read...