【问题标题】:Method to make a new twisted reactor?制造新的扭曲反应堆的方法?
【发布时间】:2014-03-16 16:15:19
【问题描述】:

我正在制作一个 IRC Log Bot,它可以按日期保存日志。我希望程序关闭当前反应器并创建一个新反应器(这是因为它会将日志保存在一个新文件中)。我写了一个示例程序,但它无法工作 -

def event():
   if no date_change:
       do normal work that has to be done
   else:
       stop present reactor
       make a new reactor

这是我正在使用的实际代码:-

def irc_NICK(self, prefix, params):
        """Called when an IRC user changes their nickname."""
        old_nick = prefix.split('!')[0]
        new_nick = params[0]
        if self.factory.filename.find(file_name_gen())!=-1:
                self.logger.log("<em>%s is now known as %s</em>" % (old_nick, new_nick),1)
        else:
                print "new itng"
                reactor.stop()
                irc.IRCClient.connectionLost(self, "Day Change")
                #earlier the LogBotFactory object is f 
                f1 = LogBotFactory("meeting-test", file_name_gen())
                reactor.connectTCP("irc.freenode.net", 6667, f1)
                reactor.run()

第二个 LogBotFactory 对象被创建,但由于一些未处理的错误,程序停止。 这是我得到的回溯......

1971-01-02 23:59:41+0530 [-] Log opened.
1971-01-02 23:59:41+0530 [-] Starting factory <__main__.LogBotFactory instance at 0x27318c0>
1971-01-03 00:00:10+0530 [LogBot,client] new itng
1971-01-03 00:00:10+0530 [LogBot,client] Starting factory <__main__.LogBotFactory instance at 0x2989cb0>
1971-01-03 00:00:10+0530 [LogBot,client] Unhandled Error
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 221, in _dataReceived
        rval = self.protocol.dataReceived(data)
      File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2412, in dataReceived
        basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
      File "/usr/lib/python2.7/dist-packages/twisted/protocols/basic.py", line 581, in dataReceived
        why = self.lineReceived(line)
      File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2420, in lineReceived
        self.handleCommand(command, prefix, params)
    --- <exception caught here> ---
      File "/usr/lib/python2.7/dist-packages/twisted/words/protocols/irc.py", line 2464, in handleCommand
        method(prefix, params)
      File "irc.py", line 141, in irc_NICK
        reactor.run()
      File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1191, in run
        self.startRunning(installSignalHandlers=installSignalHandlers)
      File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1171, in startRunning
        ReactorBase.startRunning(self)
      File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 681, in startRunning
        raise error.ReactorAlreadyRunning()
    twisted.internet.error.ReactorAlreadyRunning: 

1971-01-03 00:00:10+0530 [-] Main loop terminated.

我是 python 扭曲的新手。 请帮忙,谢谢。

【问题讨论】:

    标签: python twisted


    【解决方案1】:
    print "new itng"
    reactor.stop()
    irc.IRCClient.connectionLost(self, "Day Change")
    #earlier the LogBotFactory object is f 
    f1 = LogBotFactory("meeting-test", file_name_gen())
    reactor.connectTCP("irc.freenode.net", 6667, f1)
    reactor.run()
    

    这个问题比你想象的更容易解决。删除reactor.stop()reactor.run() 行,一切就绪。换句话说,让反应堆保持运行。

    另外,您还需要将irc.IRCClient.connectionLost(self, "Day Change") 行替换为self.loseConnection()。调用connectionLost 不会关闭连接。当 Twisted 看到连接已关闭时,它调用。如果您自己调用它,您的程序可能会认为连接已关闭,但它不会真的已关闭 - 在这种情况发生足够多的时间后,您将耗尽资源,您的程序将不会不再工作了。

    只有在使用完 Twisted 后才应该停止反应器(通常在程序退出之前)。

    【讨论】:

    • +1 表示“删除 reactor.stop() 和 reactor.run() 行,一切就绪”
    猜你喜欢
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    相关资源
    最近更新 更多