pyspider通过xmlrpc来实现webui状态和服务端的通信
# run.py
def scheduler():
if xmlrpc:
utils.run_in_thread(scheduler.xmlrpc_run, port=xmlrpc_port, bind=xmlrpc_host)
# scheduler.py
class Scheduler(object):
def xmlrpc_run(self, port=23333, bind='127.0.0.1', logRequests=False):
from pyspider.libs.wsgi_xmlrpc import WSGIXMLRPCApplication
application = WSGIXMLRPCApplication()
# 服务端注册函数
application.register_function(self.quit, '_quit')
application.register_function(self.__len__, 'size')
application.register_function(dump_counter, 'counter')
application.register_function(new_task, 'newtask')
application.register_function(send_task, 'send_task')
application.register_function(update_project, 'update_project')
application.register_function(get_active_tasks, 'get_active_tasks')
application.register_function(get_projects_pause_status, 'get_projects_pause_status')
application.register_function(webui_update, 'webui_update')
……
#wsgi_xmlrpc
from six.moves.xmlrpc_server import SimpleXMLRPCDispatcher
class WSGIXMLRPCApplication(object):
def __init__(self, instance=None, methods=None):
"""Create windmill xmlrpc dispatcher"""
self.dispatcher = SimpleXMLRPCDispatcher(allow_none=True, encoding=None)
self.dispatcher.register_instance(instance)
self.dispatcher.register_function(method)
self.dispatcher.register_introspection_functions()
def register_instance(self, instance):
return self.dispatcher.register_instance(instance)
def register_function(self, function, name=None):
return self.dispatcher.register_function(function, name)
#webui/index.py
@app.route('/counter')
def counter():
rpc = app.config['scheduler_rpc']
if rpc is None:
return json.dumps({})
result = {}
try:
data = rpc.webui_update() # 客户端调用函数
……
#run.py
def webui():
app.config['scheduler_rpc'] = connect_rpc(ctx, None, 'http://127.0.0.1:23333/')
or
app.config['scheduler_rpc'] = scheduler_rpc
def connect_rpc(ctx, param, value):
from six.moves import xmlrpc_client
return xmlrpc_client.ServerProxy(value, allow_none=True)

网友评论