【问题标题】:IRC bot make loop sleep without interrup the main loopIRC bot 在不中断主循环的情况下使循环睡眠
【发布时间】:2012-04-06 14:58:34
【问题描述】:

我一直在尝试编写一个 IRC 机器人,而我已经成功了。我在执行我想做的事情时遇到问题。代码工作正常,但我有以下问题:

由于当我使用 time.sleep(seconds) 添加第二个 While 时,机器人使用 While 循环从 IRC 读取命令,因此机器人无法连接,因为它读取了我的第二个循环并暂停连接而不是及时响应:PING 会断开连接。我一直在搜索,但越搜索越困惑,因为我不知道应该尝试什么。

无堆栈、多线程、子进程。有这么多的结果,我只是变得更加困惑。那么我正在尝试 RSS 机器人的最佳方法是什么,如果我在 IRC 频道中使用命令 !rss,机器人工作正常,但我需要它每 10 分钟检查一次新的,如果使用睡眠命令,主循环会混乱起来。

这是我的代码:

#!/usr/bin/python

import socket, sys, string, time, feedparser, hashlib
port = 6667
nick = "RSSbot"
host = 'irc.server.com'
name =  "RSSBOT"
channel = '#debug'
ident = 'rssbot'
irc = socket.socket()
irc.connect ( (host, port) )
irc.send ( 'NICK ' + nick + '\r\n' )
irc.send ( 'USER ' + ident + ' ' +  ident + ' ' + ident + ' :rssbot\r\n' )

def readRss():
    feedurl = feedparser.parse("http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=username")
    newest = feedurl['items'][0].title
    newest = newest.replace("username:","")
    msg = newest.split("http://")
    title = msg[0]
    url = msg[1]
    url = "http://" + url
    e = feedurl.entries[2]
    threadurl = e.link
    id = hashlib.md5(url + title).hexdigest()
    irc.send ("PRIVMSG #debug :Tittle:%s\r\n" % newest)
    irc.send ("PRIVMSG #debug :URL: %s\r\n" % url)
    irc.send ("PRIVMSG #debug :MD5: %s\r\n" % id)
while 1:
    data = irc.recv ( 1024 )
    print(data)

    if data.find ( '376' ) != -1:
        irc.send( 'JOIN ' + channel + '\r\n' )
    if data.find ( 'PING' ) != -1:
        irc.send( 'PONG ' + data.split() [1] + '\r\n')
    if data.find ( '!rss' ) != -1:
        feedurl = feedparser.parse("http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=username")
        newest = feedurl['items'][0].title
        newest = newest.replace("username:","")
        msg = newest.split("http://")
        title = msg[0]
        url = msg[1]
        url = "http://" + url
        #e = feedurl.entries[2]
        #threadurl = e.link
        id = hashlib.md5(url + title).hexdigest()
        irc.send ("PRIVMSG #debug :Tittle:%s\r\n" % newest)
        irc.send ("PRIVMSG #debug :URL: %s\r\n" % url)
        irc.send ("PRIVMSG #debug :MD5: %s\r\n" % id)
    while true:
        readRss()
        time.sleep(300)

如果我在 while 1: 中添加一个 while :true 与 time.sleep(300) 睡眠命令与 while 1: 循环冲突,我需要做类似的事情,所以我可以每 x 分钟检查一次新的提要.我该怎么办?

【问题讨论】:

  • 我在这里没有看到第二个循环。

标签: python sockets irc feedparser


【解决方案1】:

不要使用新的循环,而是使用单独的计时器。

import time
last_update = time.time()

while 1:
   # the rest of your while loop as usual
   now = time.time()
   if now - last_update > 300:
       # you've waited 300 seconds
       # check feeds or whatever 
       last_update = now

【讨论】:

  • 我试过了,但它不起作用我尝试了 10 秒看看它是如何进行的,但它似乎永远不会去阅读并执行它。或者也许我做错了,也许。
  • @Slightz 试试看,如果不起作用,请在您的问题中发布您尝试过的完整代码,并在此处发表评论,以便我查看并向您展示问题。如果你做对了,这个方法肯定有效
  • 是的,它现在可以工作了,谢谢,我在这里发新帖,我不知道如何正确输入代码,以为我尝试了 10 秒,但实际上需要 36 秒,但没问题。
  • @Slightz 如果data = irc.recv ( 1024 ) 阻塞,那么它就不会发生,所以你真的在设置最小值,最大值是时间加上recv 的超时时间打电话。
  • 那我该怎么办?增加方块?因为我需要机器人读取 IRC 上的命令,例如 !rss 以手动检索数据。同时自动检查背景。
【解决方案2】:

我用我的 irc 机器人上的线程模块处理了这个问题,检查项目,这可能对你有帮助 https://github.com/mouuff/MouBot(我调用了函数 ircloop,这将回答服务器 ping 并执行机器人操作:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    相关资源
    最近更新 更多