Python twisted irc:在privmsg方法中等待whois回复

Python twisted irc:在privmsg方法中等待whois回复,python,multithreading,twisted,irc,whois,Python,Multithreading,Twisted,Irc,Whois,我正在尝试使用twisted.words.protocols.IRC模块创建一个IRC机器人 bot将解析来自通道的消息,并解析它们的命令字符串 除了我需要机器人通过发送whois命令来识别尼克外,一切正常。在privmsg方法(我从中进行解析的方法)返回之前,不会处理whois回复。 例如: 有没有办法让机器人在赛尔夫之后等待回复。谁(尼克) 可能使用线程(我没有任何经验)。是Twisted的核心概念,您必须熟悉它才能使用Twisted 基本上,whois checking函数应该返回一个延迟

我正在尝试使用twisted.words.protocols.IRC模块创建一个IRC机器人

bot将解析来自通道的消息,并解析它们的命令字符串

除了我需要机器人通过发送whois命令来识别尼克外,一切正常。在privmsg方法(我从中进行解析的方法)返回之前,不会处理whois回复。
例如:

有没有办法让机器人在赛尔夫之后等待回复。谁(尼克)

可能使用线程(我没有任何经验)。

是Twisted的核心概念,您必须熟悉它才能使用Twisted


基本上,whois checking函数应该返回一个延迟的,当您收到whois回复时,该函数将被触发。

我通过将所有处理程序方法作为线程运行,然后设置一个字段来解决这个问题,如下所示 kirelagin的建议,在运行whois查询和修改接收数据的方法之前 在收到答复时更改字段。这不是最优雅的解决方案,但它很有效

修改代码:

class MyBot(irc.IRClient):
..........

    def privmsg(self, user, channel, msg):
        """This method is called when the client recieves a message"""
        if msg.startswith(':whois '):
            nick = msg.split()[1]

            self.whois_status = 'REQUEST'
            self.whois(nick)
            while not self.whois_status == 'ACK':
                sleep(1)

            print(self.whoislist)

    def irc_RPL_WHOISCHANNELS(self, prefix, params):
        """This method is called when the client recieves a reply for whois"""
        self.whoislist[prefix] = params

    def handleCommand(self, command, prefix, params):
        """Determine the function to call for the given command and call
        it with the given arguments.
        """
        method = getattr(self, "irc_%s" % command, None)
        try:
            # all handler methods are now threaded.
            if method is not None:
                thread.start_new_thread(method, (prefix, params))
            else:
                thread.start_new_thread(self.irc_unknown, (prefix, command, params))
        except:
            irc.log.deferr()

    def irc_RPL_WHOISCHANNELS(self, prefix, params):
        """docstring for irc_RPL_WHOISCHANNELS"""
        self.whoislist[prefix] = params

    def irc_RPL_ENDOFWHOIS(self, prefix, params):
        self.whois_status = 'ACK'

我想可能我没有给这个问题正确的标题,或者我没有正确理解文档,irc_RPL_whoischnels是返回数据的方法,而不是whois,但是除非调用whois的方法privmg退出,否则irc_RPL_whoischnels将永远不会执行。因此,要重新格式化我的问题,我如何运行privmsg,并且仍然让irc_RPL_whoisconnels同时运行?好的,现在让我们先忘记
延迟
s。你可以在你的类中有一个字段,它将包含一个标志“received message,waiting waiting whois reply”,当你收到whois reply时,你将检查这个标志。哦,
sleep(1)
是一个no-no-no-no-no-no。当整个Twisted都是为了避免线程时,在线程中运行……好吧……我知道这不是最好的解决方案,但这是我能想到的最好的方法。但是现在你知道我想做什么了为什么你不告诉我用twisted做这件事的最好方法呢?关于sleep(1),这只是一个例子,实际的bot更高,并且有一个超时,所以它不会永远运行。这根本不是一个解决方案。它很坏,你应该马上把它处理掉。用reactor.callInThread只处理privmsg怎么样?这对我来说也很好,因为它是做阻塞的那个。
class MyBot(irc.IRClient):
..........

    def privmsg(self, user, channel, msg):
        """This method is called when the client recieves a message"""
        if msg.startswith(':whois '):
            nick = msg.split()[1]

            self.whois_status = 'REQUEST'
            self.whois(nick)
            while not self.whois_status == 'ACK':
                sleep(1)

            print(self.whoislist)

    def irc_RPL_WHOISCHANNELS(self, prefix, params):
        """This method is called when the client recieves a reply for whois"""
        self.whoislist[prefix] = params

    def handleCommand(self, command, prefix, params):
        """Determine the function to call for the given command and call
        it with the given arguments.
        """
        method = getattr(self, "irc_%s" % command, None)
        try:
            # all handler methods are now threaded.
            if method is not None:
                thread.start_new_thread(method, (prefix, params))
            else:
                thread.start_new_thread(self.irc_unknown, (prefix, command, params))
        except:
            irc.log.deferr()

    def irc_RPL_WHOISCHANNELS(self, prefix, params):
        """docstring for irc_RPL_WHOISCHANNELS"""
        self.whoislist[prefix] = params

    def irc_RPL_ENDOFWHOIS(self, prefix, params):
        self.whois_status = 'ACK'