执行所有函数代码前的 XML-RPC 返回值 [英] XML-RPC returning value before executing all function code
本文介绍了执行所有函数代码前的 XML-RPC 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 XML-RPC 服务器:
I have XML-RPC server:
import time
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
class Worker(object):
def start_work(self):
# is it possible do return value to client here?
self.do_work()
return 'we started!'
def do_work(self):
while(True):
print 'I\'m doing work...'
time.sleep(3)
if __name__ == '__main__':
port = 8080
server = SimpleXMLRPCServer(("localhost", port))
print "Listening on port %s..." % port
w = Worker()
server.register_function(w.start_work)
while(1):
server.handle_request()
# vim: filetype=python syntax=python expandtab shiftwidth=4 softtabstop=4 encoding=utf8
简单的客户端:
import xmlrpclib
c = xmlrpclib.ServerProxy('http://localhost:8080/')
print c.start_work()
当然不会打印 start_work 函数返回的值.
Of course value returned by start_work function will be never printed.
我的问题是如何重写服务器代码以在完成工作之前返回值.我知道我可以为此使用线程,但我想确保没有更简单的方法.
My question is how to rewrite server code to make possible returning value before finishing work. I know that I can use threads for that but I want to be sure that there is no easier way.
推荐答案
如果您希望 XML-RPC 具有长时间运行、提前返回的任务,您可能需要将服务器重写为异步框架,例如 扭曲
If you want XML-RPC with long-running, early returning tasks, you probably need to rewrite your server into an asynchronous framework, like twisted
这篇关于执行所有函数代码前的 XML-RPC 返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文