在Python中睡觉时保持活动状态
第一个在Python中睡觉时保持活动状态,python,sleep,Python,Sleep,第一个如果开始送礼,然后在3秒钟后选择一个随机用户,输入IRC,并向IRC发送消息说谁赢了 但是,我的问题是,第二个if命令无法收集用户,因为第一个命令使程序休眠,而另一个if命令无法工作 我如何睡眠,但让程序能够处理其他事情? (IRC机器人) 整个代码:让第一个if子句设置一个,调用一个方法,该方法在睡眠后执行您现在执行的操作 if ":!giveaway" in prevdata and senderusr in securelist and agive == 0: agive =
如果开始送礼,然后在3秒钟后选择一个随机用户,输入IRC,并向IRC发送消息说谁赢了
但是,我的问题是,第二个if
命令无法收集用户,因为第一个命令使程序休眠,而另一个if
命令无法工作
我如何睡眠,但让程序能够处理其他事情?
(IRC机器人)
整个代码:让第一个if子句设置一个,调用一个方法,该方法在睡眠后执行您现在执行的操作
if ":!giveaway" in prevdata and senderusr in securelist and agive == 0:
agive = agive+1
message("Giveaway started by: " + senderusr)
time.sleep(3)
agive = 0
enteredgiveaway = enteredgiveaway.rstrip()
enteredgiveaway = enteredgiveaway.split(" ")
gg = random.choice(enteredgiveaway)
message("The winner of the giveaway is: " + gg)
gg = ""
enteredgiveaway = ""
if "PRIVMSG" in prevdata and agive == 1:
enteredgiveaway += senderusr + " "
在您引用的代码段中:
from threading import Timer
def giveaway():
agive = 0
enteredgiveaway = enteredgiveaway.rstrip()
enteredgiveaway = enteredgiveaway.split(" ")
gg = random.choice(enteredgiveaway)
message("The winner of the giveaway is: " + gg)
gg = ""
enteredgiveaway = ""
这有点粗略,因为我不知道在代码示例中,enteredgiveaway
来自哪里。因此,根据代码的其余部分,您可能需要重新组织/重组一点以使其工作。如果您还没有这样做,那么最好将所有内容都放在一个类中并使用实例变量。让第一个if子句设置一个,调用一个方法,该方法在睡眠后执行您现在执行的操作
if ":!giveaway" in prevdata and senderusr in securelist and agive == 0:
agive = agive+1
message("Giveaway started by: " + senderusr)
time.sleep(3)
agive = 0
enteredgiveaway = enteredgiveaway.rstrip()
enteredgiveaway = enteredgiveaway.split(" ")
gg = random.choice(enteredgiveaway)
message("The winner of the giveaway is: " + gg)
gg = ""
enteredgiveaway = ""
if "PRIVMSG" in prevdata and agive == 1:
enteredgiveaway += senderusr + " "
在您引用的代码段中:
from threading import Timer
def giveaway():
agive = 0
enteredgiveaway = enteredgiveaway.rstrip()
enteredgiveaway = enteredgiveaway.split(" ")
gg = random.choice(enteredgiveaway)
message("The winner of the giveaway is: " + gg)
gg = ""
enteredgiveaway = ""
这有点粗略,因为我不知道在代码示例中,enteredgiveaway
来自哪里。因此,根据代码的其余部分,您可能需要重新组织/重组一点以使其工作。如果您还没有这样做,那么最好将所有内容都放在一个类中并使用实例变量。我认为这基本上就是我想要的。但是,我需要编辑它来使用我的代码。将再次回复,如果我得到它与此工作。更新了我的全部代码的主要职位。使用您的代码,我得到:线程线程1中的异常:回溯(最近一次调用):文件“C:\Python27\lib\threading.py”,第551行,在uuu bootstrap\u internal self.run()文件“C:\Python27\lib\threading.py”第755行,在run self.function(*.args,**self.kwargs)文件“updateing.py”第156行,在Giveway enteredgiveaway=enteredgiveaway.rstrip()UnboundLocalError:分配前引用了局部变量“enteredgiveaway”,不确定原因?函数不能访问全局变量吗?它们可以,但只有在您告诉它们时才可以。最简单的(不是最干净的…)修复方法是在giveaway()的开头添加global enteredgiveaway
,我想这基本上就是我想要的。但是,我需要编辑它来使用我的代码。将再次回复,如果我得到它与此工作。更新了我的全部代码的主要职位。使用您的代码,我得到:线程线程1中的异常:回溯(最近一次调用):文件“C:\Python27\lib\threading.py”,第551行,在uuu bootstrap\u internal self.run()文件“C:\Python27\lib\threading.py”第755行,在run self.function(*.args,**self.kwargs)文件“updateing.py”第156行,在Giveway enteredgiveaway=enteredgiveaway.rstrip()UnboundLocalError:分配前引用了局部变量“enteredgiveaway”,不确定原因?函数不能访问全局变量吗?它们可以,但只有在您告诉它们时才可以。最简单的(不是最干净的…)修复方法是在giveaway()的开头添加global enteredgiveaway