supervisor 重启_supervisor_twiddler的使用
點擊上方藍字關(guān)注【 北郵郭大寶 】
Supervisor可以很好的實現(xiàn)Python的進程管理,但是新增進程時需要配置文件,對于需要動態(tài)創(chuàng)建進程的場景不是很友好,supervisor_twiddler插件可以幫助實現(xiàn)上述功能。本文就supervisor_twiddler的使用做簡單的demo,詳細內(nèi)容可以查閱https://github.com/mnaberez/supervisor_twiddler
1. 安裝?
pip install supervisor_twiddler? ?2. 準備待管理的進程
這里使用一個faskapi的demo做為稍后supervisor管理的進程
from fastapi import FastAPIapp = FastAPI()@app.get("/")async def hello():??return?"hello?world, guoxingyu"3. 修改supervisord.conf
supervisord.conf的改動有兩個:第一是目前看supervisor_twiddler推薦給已有的組添加進程,所以需要在supervisord.conf中添加組名;第二是添加[rpcinterface:twiddler],內(nèi)容如下:
[group:test][rpcinterface:twiddler]supervisor.rpcinterface_factory = supervisor_twiddler.rpcinterface:make_twiddler_rpcinterface4. 通過supervisor_twiddler實現(xiàn)進程管理
from xmlrpc.client import ServerProxyif __name__ == '__main__': # 通過user、passwd連接supervisor的RPC服務(wù) server = ServerProxy("http://user:123@localhost:9001/RPC2") # 打印目前的Group list print(server.twiddler.getGroupNames()) # 動態(tài)新增process server.twiddler.addProgramToGroup('test', 'helloworld', \ {'command': 'gunicorn fastapidemo:app -b 0.0.0.0:8088 -w 1 -k uvicorn.workers.UvicornWorker', 'autostart':'true', 'autorestart':'true'}) # 停止process并刪除 server.supervisor.stopProcess("test:helloworld") server.twiddler.removeProcessFromGroup("test", "helloworld") # 查看process日志 print(server.supervisor.tailProcessStderrLog("test:helloworld",0,512))這里主要實現(xiàn)了動態(tài)新增進程,關(guān)閉進程,查看日志三個子功能。
其中addProgramToGroup的具體參數(shù)可以看Github原文,第一個是Group名、第二個是process名,第三個是dict,包括supervisor配置的一些參數(shù)。
效果:
不過supervisor_twiddler管理的進程不能持久化保存,當supervisor重啟后,進程就不在了,需要重新動態(tài)添加進程。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的supervisor 重启_supervisor_twiddler的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业门户网站服务器,企业或个人门户网站对
- 下一篇: 傲梦python笔试题_python笔试