python - 功能在另一个具有睡眠的功能中不起作用
问题描述
我有一个函数可以抓取网站并返回一个语句,这取决于它是否找到了某些关键字。此功能称为检查站点。当我自己运行该函数时,它工作得很好,但我无法让它与 time.sleep 一起在另一个函数中工作。
这很好用
checksite()
这不起作用
while True:
checksite()
time.sleep(10)
我希望检查站点功能每 10 秒运行一次。感谢所有帮助!
解决方案
了解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))
推荐阅读
- c++ - 如何使用isolate->terminateExecution 停止所有线程
- arrays - 如何合并嵌套数组中的哈希?
- c# - Visual Studio 2019 事件处理程序自动完成建议
- reactjs - 如何在 gatsby 的特定页面上实现 zendesk Web 小部件
- ios - iOS:如何减少情节提要和代码重复?
- cassandra - cassandra 上的低基数列上的多个索引如何工作?
- jekyll - Jekyll -- gems 需要两个不同版本的站点地图?
- c# - how to read a file and in that file it has many numbers it get split by "," and i want to calculate GCD and LCM
- angular - 如何从 Angular Material Dialog 调用另一个组件函数
- c++ - Codelite C++ 新项目