首页 > 解决方案 > 功能在另一个具有睡眠的功能中不起作用

问题描述

我有一个函数可以抓取网站并返回一个语句,这取决于它是否找到了某些关键字。此功能称为检查站点。当我自己运行该函数时,它工作得很好,但我无法让它与 time.sleep 一起在另一个函数中工作。

这很好用

    checksite()

这不起作用

   while True:
        checksite()
        time.sleep(10)

我希望检查站点功能每 10 秒运行一次。感谢所有帮助!

标签: python

解决方案


了解checktime()执行的作用很重要。

如果您在 10 秒后没有看到任何事情发生并且脚本执行的时间仍然超出预期,我的第一个建议是了解执行需要多长时间。

您可以运行它并获得时间量:

import time
import datetime

def checktime():
    #Doing some execution
    print('execution...')

#Use:
while True:
    started = datetime.datetime.now()

    checktime()
    time.sleep(10)

    executed = datetime.datetime.now()

    print('The script runtime is: {0}'.format(executed - started))

推荐阅读