2013-10-08 19 views
0

是否有办法自动执行周期性任务:如使用python irclibtwisted-based youmomdotcom python irc bot将消息发送给其他用户或通道等。使用python irc库定期执行任务

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

回答

1

由于雕文指出的那样,我已经覆盖irc客户端类的实例方法connectionMade并使其使用LoopingCall

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

如果您使用基于双绞线的解决方案,你可以简单地用一个LoopingCall安排你想调用任何周期性的方法。

(如果你使用irclib这是更难做到这一点的是,在所有情况下正常工作的一种方式,所以我将不包括在我的答案在这里。)