sleep()函数(python)

  if data.find('!google') != -1:
     nick = data.split('!')[ 0 ].replace(':','')
     try:
       gs = GoogleSearch(args)
       gs.results_per_page = 1
       results = gs.get_results()
       for res in results:
         sck.send('PRIVMSG ' + chan + " " + res.title.encode("utf8") + 'rn')
         sck.send('PRIVMSG ' + chan + " " + res.url.encode("utf8") + 'rn')
         print
     except SearchError, e:
       sck.send('PRIVMSG ' + chan + " " + "Search failed: %s" % e + " " + 'rn')
好吧我正在尝试使脚本等待几秒钟,然后另一个用户可以“!google”以防止用户充斥渠道或机器人,不确定我是否应该使用sleep()函数,因为这可能会停止整个脚本,我只是想让它等几秒才能再次使用“!google”。     
已邀请:
time
模块内部有一个
sleep
功能。 但是,要使脚本不被阻止,可以调用
time
模块中的
time
函数并存储它。如果当前时间小于加,例如五秒,则不允许他们使用它。 例如:
last_google = 0
# somewhere later in the script where last_google is still in scope...
if data.find('!google') != -1:
    if last_google + 5 < time.time():
        # throttled
        return
    last_google = time.time()
    # do something here
    

要回复问题请先登录注册