1

这是一个说明我的问题的示例程序。该程序启动一个 wxPython 应用程序并在一个线程中启动一个 SimpleXMLRPCServer。这一切都很好。我的问题是我无法关闭 SimpleXMLRPCServer 线程,因为它在 handle_request() 调用中被阻塞。我在 Windows XP 机器上开发(不知道在 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()

从我的在线研究中,我可以看到杀死线程是一个麻烦的问题。

看来我的选择是扭曲或处理模块......还有其他解决方案吗?

这是一篇我认为非常有趣的帖子,尽管我认为它不会对我有帮助,因为我可能在套接字而不是 python 中被阻塞: http ://www.velocityreviews.com/forums/t330554-kill- a-thread-in-python.html

4

2 回答 2

1

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()
于 2010-02-04T03:25:21.397 回答
0

我不确定它是否仍然令人感兴趣,但如果代码有效

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

被替换为

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

使用这些行 self.server.server_close() 将在 server_forever() 中引发错误,这将退出它并完成线程。

于 2015-07-28T21:07:19.663 回答