开发者

SimpleXMLRPCServer, wxPython and Thread - Howto Stop?

Here is an example program that illustrates my problem. The program starts a wxPython application and starts a SimpleXMLRPCServer in a thread. This all works fine. My problem is that I can't shut down the SimpleXMLRPCServer thread because it is blocked on the handle_request() call. I am developing on a Windows XP machine (I don't know if the same problem occurs on linux).

import wx
import SimpleXMLRPCServer
import threading

class myServerFunction(object):
    def result(self):
        return "Hello World"

class serverThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()      

    def stop(self):    
        self.server.server_close()
        self.timeToQuit.set()

    def run(self):
        self.server = SimpleXMLRPCServer.SimpleXMLRPCServer( ("localhost", 8000), logRequests=False )
        self.server.register_instance( myServerFunction )
        #self.server.serve_forever()
        while not self.timeToQuit.isSet():
            self.server.get_request()
            self.server.handle_request()

class MyFrame(wx.Frame):

    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.serverThread = serverThread()
        self.serverThread.start() 
        wx.EVT_CLOSE(self, self.OnExit)   

    def OnExit(self, event):
        print "Server should turn off!"
        self.serverThread.stop()
        self.Destroy()


app = wx.PySimpleApp(0)
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
app.Exit()

From my online research, I can see that killing threads is a troublesome issue.

I开发者_如何学运维t seems my options are twisted or processing module... Is there another solution?

Here is one post that I thought was unusually interesting, although i don't think it will help me as I am probably blocked at the socket and not in python: http://www.velocityreviews.com/forums/t330554-kill-a-thread-in-python.html


This works. Credit goes to the link in my above comment.

import wx
import SimpleXMLRPCServer
import threading
import xmlrpclib

class myServerFunction(object):
    def result(self):
        print "myServerFunction"
        return "Hello World"

class serverThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()      

    def stop(self):    
        self.server.server_close()
        self.timeToQuit.set()

    def run(self):
        print "runing"
        self.server = SimpleXMLRPCServer.SimpleXMLRPCServer( ("localhost", 8000), logRequests=False )
        self.server.register_instance( myServerFunction() )
        while not self.timeToQuit.isSet():
            self.server.handle_request()

class MyFrame(wx.Frame):

    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.serverThread = serverThread()
        self.serverThread.start() 
        wx.EVT_CLOSE(self, self.OnExit)   
        self.server = xmlrpclib.Server( "http://localhost:8000" )

    def OnExit(self, event):
        print "Server should turn off!"
        self.serverThread.stop()
        print self.server.result() # dummy call to unlock the socket deadlock
        self.Destroy()

app = wx.PySimpleApp(0)
frame_1 = MyFrame(None, -1, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
app.Exit()


I am not sure whether it is still of interest, but the code works if

while not self.timeToQuit.isSet():
    self.server.handle_request()

is replaced by

try:
    self.server.serve_forever()
except:
    print( "SimpleXMLRPCServer stopped"  )

Using these lines self.server.server_close() will raise an error in server_forever() which will exit it and finish the thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜