【问题标题】:Perform tasks periodically with python irc library使用 python irc 库定期执行任务
【发布时间】:2013-10-08 22:45:32
【问题描述】:

有没有办法自动执行周期性任务:例如使用 python irclibtwisted-based youmomdotcom python irc bot 向其他用户或频道发送消息等。

基于 irclib 的 irc 客户端示例:

from irc import client
class OurIRCClient(client.SimpleIRCClient):
    def __init__(self):
        client.SimpleIRCClient.__init__(self)

import sys
client = OurIRCClient()

try:
    client.connect("irc.freenode.net", 6667, myUserId)
    print "connected to irc.freenode.net"
except:
    sys.exit(-1)
    "error: coouldn't connect to irc server"
client.connection.join("#django-hotclub")
client.start()

【问题讨论】:

    标签: python twisted irc


    【解决方案1】:

    如果您使用基于 Twisted 的解决方案,您可以简单地使用 LoopingCall 来安排您想要调用的任何周期性方法。

    (如果您使用 irclib,则很难以在所有情况下都能正常工作的方式执行此操作,因此我不会在此处将其包含在答案中。)

    【讨论】:

      【解决方案2】:

      正如 Glyph 指出的,我已经覆盖了 irc 客户端类的实例方法 connectionMade 并使其使用LoopingCall

       def connectionMade(self):
              irc.IRCClient.connectionMade(self)
              task.LoopingCall(lambda : (self.msg(counterpartID, "hi there"))).start(5.0)
      

      【讨论】:

        猜你喜欢
        • 2012-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 2017-04-11
        • 2012-08-02
        • 1970-01-01
        相关资源
        最近更新 更多