4

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. seem server.serve_forever() is running forever there.

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.

Thank you for your kind of help.

2 Answers 2

8

You could create a ServerThread class to encapsulate your XML-RPC server and run it in a thread :

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..."
Sign up to request clarification or add additional context in comments.

2 Comments

How about marking Xion's response as answering your question?
make sure to set daemon=True if you want the server thread to auto-exit when the main process is closed
2

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.