简体   繁体   English

sleep()函数(python)

[英]sleep() function (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") + '\r\n')
         sck.send('PRIVMSG ' + chan + " " + res.url.encode("utf8") + '\r\n')
         print
     except SearchError, e:
       sck.send('PRIVMSG ' + chan + " " + "Search failed: %s" % e + " " + '\r\n')

Ok I'm trying to make the script wait a few seconds before another user can "!google" to prevent users from flooding the channel or the bot, not sure if I should use the sleep() function because that might stop the whole script, I just want to make it wait a few seconds before anyone can use "!google" again. 好吧我正在尝试使脚本等待几秒钟,然后另一个用户可以“!google”以防止用户充斥频道或机器人,不确定我是否应该使用sleep()函数,因为这可能会停止整个脚本,我只是想让它等几秒才能再次使用“!google”。

There is a sleep function inside the time module . time模块中有一个sleep功能

However, to make your script not block, you can call the time function in the time module and store that. 但是,要使脚本不被阻止,可以在time模块中调用time函数并存储它。 If the current time is less than that plus, say, five seconds, don't allow them to use it. 如果当前时间小于加,例如五秒,则不允许他们使用它。

For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM