搜尋
首頁後端開發Python教學Python使用Supervisor来管理进程的方法

本文实例讲述了Python使用Supervisor来管理进程的方法。分享给大家供大家参考。具体分析如下:

Supervisor可以启动、停止、重启*nix系统中的程序。也可以重启崩溃的程序。

supervisord的一个守护进程,用于将指定的进程当做子进程来运行。

supervisorctl是一个客户端程序,可以查看日志并通过统一的会话来控制进程。

看例子:

我们写了一个py脚本,用于往log文件中记录一条当前的时间。

root@ubuntu:/home/zoer# cat daemon.py
#!/usr/bin/env python
import time
import os
time.sleep(1)
f=open("log",'a')
t=time.time()
f.write(str(t))
f.write("\n")
f.close()

安装过程就不说了。

安装完毕supervisor之后【将配置文件放在/etc下】。修改配置文件,在最后增加如下内容:

[program:ddd]
command=/home/zoer/daemon.py
autorestart=true

然后我们启动supervisor并启动daemon.py的执行。

root@ubuntu:/home/zoer# supervisord 
/usr/local/lib/python2.7/dist-packages/supervisor-3.0b1-py2.7.egg/supervisor/options.py:286: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security. 
 'Supervisord is running as root and it is searching ' 
root@ubuntu:/home/zoer# supervisorctl 
ddd               STARTING   
supervisor> start ddd 
ddd: ERROR (already started) 
supervisor> stop ddd 
ddd: stopped 
supervisor> start ddd 
ddd: started 
supervisor>

从上面的例子中,看到,可以通过start或者stop命令来启动或者停止ddd这个进程。ddd这里就是我们在配置文件中增加的内容(daemon.py这个脚本)。

也可以使用restart。如下:

supervisor> restart  ddd
ddd: stopped
ddd: started

下面我们测试一下,假设说我们手动kill掉了ddd这个进程,那么ddd会自动恢复执行吗?

为了做实验,把代码修改如下:

root@ubuntu:/home/zoer# cat daemon.py  
#!/usr/bin/env python 
import time 
import os 
while True: 
  time.sleep(1) 
  f=open("log",'a') 
  t=time.time() 
  f.write(str(t)) 
  f.write("\n") 
  f.close()

通过ps可以找到这个进程的id:

root   9354 0.2 0.4 10924 4200 ?    S  23:16  0:00 python /home/zoer/daemon.py 
root   9395 0.0 0.0  4392  832 pts/3  S+  23:17  0:00 grep --color=auto daemon 
root@ubuntu:/home/zoer#

看下面的操作:

root@ubuntu:/home/zoer# rm log;touch log;kill 9354
root@ubuntu:/home/zoer# cat log 
1364710712.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
1364710714.52 
root@ubuntu:/home/zoer# cat log 
1364710712.51 
1364710713.51 
1364710714.52 
1364710715.52

删除了log文件,并且重新创建。然后干掉了daemon.py的那个进程。会发现log内容又重新有新的内容了。再次ps查看进程号。

root   9429 0.1 0.4 10924 4200 ?    S  23:18  0:00 python /home/zoer/daemon.py 
root   9440 0.0 0.0  4392  828 pts/3  S+  23:19  0:00 grep --color=auto daemon 
root@ubuntu:/home/zoer#

会发现进程号已经变成9429了。说明supervisor已经重启了被干掉了的进程。

希望本文所述对大家的Python程序设计有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

您如何在python列表中訪問元素?您如何在python列表中訪問元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科學計算中如何使用陣列?Python的科學計算中如何使用陣列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何處理同一系統上的不同Python版本?您如何處理同一系統上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

與標準Python陣列相比,使用Numpy數組的一些優點是什麼?與標準Python陣列相比,使用Numpy數組的一些優點是什麼?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具