本文实例讲述了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程序设计有所帮助。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

Dreamweaver Mac版
ビジュアル Web 開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ホットトピック









