time() 函数(python)

发布于 2024-10-31 10:26:05 字数 873 浏览 7 评论 0原文

    if data.find('!google') != -1:
     nick = data.split('!')[ 0 ].replace(':','')
     if last_google + 5 > time.time():
        sck.send('PRIVMSG ' + chan + " :" + ' Wait a few seconds' + "\r\n")
     else:
           last_google = time.time()
           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')

好吧,我试图让脚本在另一个用户可以“!google”之前等待几秒钟,以防止用户淹没频道或机器人。显然这个脚本不起作用,我错误地实现了时间函数,或者我'我缺少一些东西

    if data.find('!google') != -1:
     nick = data.split('!')[ 0 ].replace(':','')
     if last_google + 5 > time.time():
        sck.send('PRIVMSG ' + chan + " :" + ' Wait a few seconds' + "\r\n")
     else:
           last_google = time.time()
           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.. Obviously this script doesnt work, I'm implementing the time function wrong or I'm missing something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2024-11-07 10:26:05

这一切都在循环中吗?

一个问题是您在请求开始时将 last_google 设置为 time.time() 。如果请求很慢,那么当您再次到达 if 语句时,部分时间可能已经过去了。

典型的“等待”块可能看起来像这样:

while time.time() < last_google + 5:
    sleep(1) # You don't want to keep using the cpu, so let your process sleep

Is this all in a loop?

One issue is that you set last_google to time.time() at the beginning of your request. If the request is slow, some of that time may already be gone by the time you get to the if statement again.

A typical 'waiting' block might look something like this:

while time.time() < last_google + 5:
    sleep(1) # You don't want to keep using the cpu, so let your process sleep
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文