SimpleXMLRPCServer、wxPython 和线程 - 如何停止?
这是一个说明我的问题的示例程序。该程序启动一个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
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.
It 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效。归功于我上面评论中的链接。
This works. Credit goes to the link in my above comment.
我不确定它是否仍然令人感兴趣,但是如果
替换为
使用这些行 self.server.server_close() 将在 server_forever() 中引发错误,该错误将退出并完成线程,则代码可以工作。
I am not sure whether it is still of interest, but the code works if
is replaced by
Using these lines self.server.server_close() will raise an error in server_forever() which will exit it and finish the thread.