SimpleXMLRPCServer、wxPython 和线程 - 如何停止?

发布于 2024-08-20 01:27:06 字数 1769 浏览 7 评论 0原文

这是一个说明我的问题的示例程序。该程序启动一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-08-27 01:27:06

这有效。归功于我上面评论中的链接。

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()

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()
殤城〤 2024-08-27 01:27:06

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

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

替换为

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

使用这些行 self.server.server_close() 将在 server_forever() 中引发错误,该错误将退出并完成线程,则代码可以工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文