[python基础]xml_rpc远程调控supervisor节点进程

supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的。

xml_rpc其实就是本地可以去调用远端的函数方法,在python中只需要引入xmlrpclib即可实现对客户端的操作

例如:

import xmlrpclib
server = xmlrpclib.server('http://localhost:9001/RPC2')
methods = server.system.listMethods()
print(methods)

结果如下:

['supervisor.addProcessGroup', 'supervisor.clearAllProcessLogs', 'supervisor.clearLog', 'supervisor.clearProcessLog', 

'supervisor.clearProcessLogs', 'supervisor.getAPIVersion', 'supervisor.getAllConfigInfo', 'supervisor.getAllProcessInfo',

'supervisor.getIdentification', 'supervisor.getPID', 'supervisor.getProcessInfo', 'supervisor.getState', 'supervisor.getSupervisorVersion',

'supervisor.getVersion', 'supervisor.readLog', 'supervisor.readMainLog', 'supervisor.readProcessLog', 'supervisor.readProcessStderrLog',

'supervisor.readProcessStdoutLog', 'supervisor.reloadConfig', 'supervisor.removeProcessGroup', 'supervisor.restart', 'supervisor.sendProcessStdin',

'supervisor.sendRemoteCommEvent', 'supervisor.shutdown', 'supervisor.signalAllProcesses', 'supervisor.signalProcess', 'supervisor.signalProcessGroup',

'supervisor.startAllProcesses', 'supervisor.startProcess', 'supervisor.startProcessGroup', 'supervisor.stopAllProcesses', 'supervisor.stopProcess',

'supervisor.stopProcessGroup', 'supervisor.tailProcessLog', 'supervisor.tailProcessStderrLog', 'supervisor.tailProcessStdoutLog',

'system.listMethods', 'system.methodHelp', 'system.methodSignature', 'system.multicall']

所有方法在/Library/Python/2.7/site-packages/supervisor/rpcinterface.py下

举个例子:

1.启动该节点下某个程序:
if
server.supervisor.startProcess(process_name): print('%s has started' %process_name)

如果该程序已经启动会报错:

  xmlrpclib.Fault: <Fault 60: 'ALREADY_STARTED: celery_beat'>

  2.停止某个程序

server.supervisor.stopProcess(process_name)

 如果该程序未启动则会报错:

  xmlrpclib.Fault: <Fault 70: 'NOT_RUNNING: celery_beat'>

  3.读取日志

  og = server.supervisor.readLog(0,500)

  print(log)

结果如下:

2018-09-14 17:31:25,782 INFO RPC interface 'supervisor' initialized
2018-09-14 17:31:25,782 CRIT Server 'inet_http_server' running without any HTTP authentication checking
2018-09-14 17:31:25,785 INFO daemonizing the supervisord process
2018-09-14 17:31:25,786 INFO supervisord started with pid 51752
2018-09-14 17:31:58,968 INFO spawned: 'django_web' with pid 51757
2018-09-14 17:32:00,512 INFO success: django_web entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2018-0

4.关闭某个supervisor节点
server.supervisor.shutdown()
此时如果再用xmlrpclib创建server会报错:

  socket.error: [Errno 61] Connection refused

完。

posted @ 2018-09-19 14:54  赞美上帝  阅读(1099)  评论(0编辑  收藏  举报