首页 文章

制造新型扭曲反应器的方法?

提问于
浏览
1

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

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对象被创建但是程序由于某些Unhandled错误而停止 . 这是我得到的追溯......

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 twisted的新手 . 请帮忙,谢谢 .

1 回答

  • 2
    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看到连接已关闭时,它会被调用 . 如果你自己调用它,你的程序可能会认为连接已经关闭但它实际上并没有关闭 - 在这种情况发生之后,你已经足够多了'll be out of resources and your program won' .

    您应该只在使用Twisted时停止反应器(通常在程序退出之前) .

相关问题