【问题标题】:Unclean exist with twisted+wxPython不干净存在于twisted+wxPython
【发布时间】:2012-11-12 23:48:02
【问题描述】:

我正在使用twistd。这是应用程序中唯一的服务:

class GameClientService(internet.TCPClient):
    def __init__(self, serverHost, serverPort):
        self.wxApp = wx.App(False)
        reactor.registerWxApp(self.wxApp)
        self.lobbyFrame = gui.LobbyFrame()

        internet.TCPClient.__init__(self, serverHost, serverPort, LobbyProtocolFactory(self.lobbyFrame))

    def stopService(self):
        internet.TCPClient.stopService(self)
        print "Stop service!"
        destroyedD = defer.Deferred()
        self.lobbyFrame.CloseWithCallback(destroyedD.callback, True)
        print "close called!"
        def fired(result):
            print "'Destroyed' deferred has fired with %s" % (result,)
        destroyedD.addCallback(fired)
        return destroyedD

CloseWithCallbackwx.Frame 上的定义如下:

def CloseWithCallback(self, callback, *callbackArgs):
    def destroyed(event):
        event.Skip()
        callback(*callbackArgs)
    self.Bind(wx.EVT_WINDOW_DESTROY, destroyed)
    self.Close()

LobbyProtocol的工厂,如果连接失败,我会停止反应器:

def clientConnectionFailed(self, connector, reason):
    print "Client connection failed: %s" % reason.getErrorMessage()
    reactor.stop()

我在没有监听服务器的情况下运行客户端,因此连接失败,但有时(可能超过一半,但并非总是如此):

2012-11-12 18:43:29-0500 [-] Started connecting <twisted.internet.tcp.Connector instance at 0x030E3800>
2012-11-12 18:43:30-0500 [Uninitialized] Client connection failed: Connection was refused by other side: 10061: No connection could be made because the target machine actively refused it..
2012-11-12 18:43:30-0500 [Uninitialized] Stopping factory <network.LobbyProtocol.LobbyProtocolFactory instance at 0x030E3698>
2012-11-12 18:43:30-0500 [-] Stop service!
2012-11-12 18:43:30-0500 [-] 'Destroyed' deferred has fired with True
2012-11-12 18:43:30-0500 [-] Traceback (most recent call last):
2012-11-12 18:43:30-0500 [-]   File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 14669, in <lambda>
2012-11-12 18:43:30-0500 [-]     lambda event: event.callable(*event.args, **event.kw) )
2012-11-12 18:43:30-0500 [-]   File "C:\Python26\lib\site-packages\twisted\internet\_threadedselect.py", line 232, in _interleave
2012-11-12 18:43:30-0500 [-]     msg, args = self.toMainThread.get_nowait()
2012-11-12 18:43:30-0500 [-]   File "C:\Python26\lib\Queue.py", line 190, in get_nowait
2012-11-12 18:43:30-0500 [-]     return self.get(False)
2012-11-12 18:43:30-0500 [-]   File "C:\Python26\lib\Queue.py", line 165, in get
2012-11-12 18:43:30-0500 [-]     raise Empty
2012-11-12 18:43:30-0500 [-] Queue.Empty
2012-11-12 18:43:30-0500 [-] Server Shut Down.
2012-11-12 18:43:30-0500 [-] Server Shut Down.

这让我很不舒服。如何确保回溯永远不会运行?我做错了什么?

【问题讨论】:

  • 听起来您正在使用 wxreactor(尽管您没有明确表示)。由于 Twisted 社区成员缺乏兴趣,wxreactor 实际上没有得到维护。如果您对它感兴趣,那么您可能会从为其提供一些维护工作中受益。例如,如果您发现似乎是错误的不良行为(根据您粘贴的代码,这似乎是),那么您应该提交错误报告并附上补丁来解决问题(您至少应该做前者,所以人们会知道有一个错误;如果你真的想修复它,后者也可能是必要的)。
  • @Jean-PaulCalderone:啊,谢谢你的提示!贡献有用的东西的想法很有吸引力。如果我有时间,我会这样做

标签: python wxpython twisted twistd


【解决方案1】:

我已经进行了适当的清洗,因此我的代码现在被净化了:

class CleanExitApp(wx.App):
    def __init__(self, *args, **kwargs):
        wx.App.__init__(self, *args, **kwargs)

        self.exitDeferreds = []

    def AddExitDeferred(self, exitDeferred):
        self.exitDeferreds.append(exitDeferred)

    def OnExit(self):
        print "OnExit"
        for exitDeferred in self.exitDeferreds:
            exitDeferred.callback(True)

class GameClientService(internet.TCPClient):
    def __init__(self, serverHost, serverPort):
        self.wxApp = CleanExitApp(False)
        reactor.registerWxApp(self.wxApp)
        self.lobbyFrame = gui.LobbyFrame() 

        internet.TCPClient.__init__(self, serverHost, serverPort, LobbyProtocolFactory(self.lobbyFrame))

    def stopService(self):
        internet.TCPClient.stopService(self)
        print "Stop service!"
        exitD = defer.Deferred()
        self.wxApp.AddExitDeferred(exitD)
        self.lobbyFrame.Close()
        print "close called!"
        def fired(result):
            print "'Destroyed' deferred has fired with %s" % (result,)
        exitD.addCallback(fired)
        return exitD

输出使我们感到高兴:

2012-11-12 18:56:15-0500 [-] Started connecting <twisted.internet.tcp.Connector instance at 0x032AB8C8>
2012-11-12 18:56:16-0500 [Uninitialized] Client connection failed: Connection was refused by other side: 10061: No connection could be made because the target machine actively refused it..
2012-11-12 18:56:16-0500 [Uninitialized] Stopping factory <network.LobbyProtocol.LobbyProtocolFactory instance at 0x032AB7B0>
2012-11-12 18:56:16-0500 [-] Stop service!
2012-11-12 18:56:16-0500 [-] close called!
2012-11-12 18:56:16-0500 [-] OnExit
2012-11-12 18:56:16-0500 [-] 'Destroyed' deferred has fired with True
2012-11-12 18:56:16-0500 [-] Server Shut Down.
2012-11-12 18:56:16-0500 [-] Server Shut Down.

赞美主!

人们只是想知道领主是否有不同的信息......一个更内置的信息,不需要如此折磨的代码?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多