简体   繁体   English

Python多线程XMLRPC服务器(?)

[英]Python Multithreading XMLRPC server (?)

Basicly I want to run my xmlrpc server in separate thread or together with my other code, however, after server.serve_forever() there's no way that I can get my another code running after this function. 基本上我想在单独的线程中运行我的xmlrpc服务器或者与我的其他代码一起运行,但是,在server.serve_forever()之后,我无法在此函数之后运行另一个代码。 seem server.serve_forever() is running forever there. 似乎server.serve_forever()在那里永远运行。

self.LocalServer = SimpleThreadedXMLRPCServer(("localhost",10007))
self.LocalServer.register_function(getTextA) #just return a string
self.LocalServer.serve_forever()
print "I want to continue my code after this..."
.... another code after this should running together with the server

I tried the multithreading concept but still no luck here. 我尝试了多线程概念,但在这里仍然没有运气。 Basicaly I want to run the xmlrpc server together with the rest of my code. Basicaly我想和我的其余代码一起运行xmlrpc服务器。

Thank you for your kind of help. 谢谢你的帮助。

You could create a ServerThread class to encapsulate your XML-RPC server and run it in a thread : 您可以创建一个ServerThread类来封装XML-RPC服务器并在线程中运行它:

class ServerThread(threading.Thread):
    def __init__(self):
         threading.Thread.__init__(self)
         self.localServer = SimpleThreadedXMLRPCServer(("localhost",10007))
         self.localServer.register_function(getTextA) #just return a string

    def run(self):
         self.localServer.serve_forever()

You can use this class the following way : 您可以通过以下方式使用此类:

server = ServerThread()
server.start() # The server is now running
print "I want to continue my code after this..."

I wanted to do the same thing as you and this is how I do it. 我想和你做同样的事情,这就是我做的事情。

server = SimpleXMLRPCServer(('127.0.0.1', 9000), logRequests=True, allow_none=True)
server.register_instance(ServerTrial()) 
server.register_introspection_functions()
server.register_multicall_functions()
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
print'This will be printed'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM