[英]how to execute a program for every ten mins for the entire day in Python
I am working on web crawler app to download the stock price every ten minutes. 我正在使用网络爬虫应用程序,每十分钟下载一次股价。 I am able to extract the quote but I am not sure how to schedule it to run every ten minutes for the entire day. 我能够提取报价,但是我不确定如何安排它在一整天内每十分钟运行一次。 Please suggest me either time loop kind of a thing or the solution for the web crawler app itself. 请向我建议时间循环之类的东西,或者为Web爬虫应用程序本身提供解决方案。
I need a solution which works on Windows. 我需要在Windows上可以使用的解决方案。
you can do try this 你可以试试看
import time
and but this code before dawnload method 但是这段代码在破晓方法之前
time.sleep(10*60) #this will stop the program for 10 minutes
Use crontab. 使用crontab。 Open the crontab file via terminal: 通过终端打开crontab文件:
$ crontab -e
At the end of the file you can set the python script and the frequency of running it. 在文件末尾,您可以设置python脚本及其运行频率。 For example to run a script every 10 mins: 例如,每10分钟运行一次脚本:
*/10 * * * * /path/to/script
Ok, so you have a python function f() which you want to execute every ten mins for a day. 好的,所以您有一个python函数f(),希望每天执行一次,每十分钟一次。 You can do this: 你可以这样做:
import time
while True: #Infinite loop
f() #Execute the function
time.sleep(600) #Wait 600s (10 min) before re-entering the cycle
And you keep the script running for as long as you need (a day, a week, whatever, it will not close by itself) 并且您可以将脚本保持运行所需的时间(一天,一周,任何时间,它不会自行关闭)
Windows has a Task Scheduler built-in. Windows具有内置的任务计划程序。 You can use that to run your script: 您可以使用它来运行脚本:
Otherwise, you can write an infinite loop that will check the stock prices every ten minutes using something like time.sleep(600)
. 否则,您可以编写一个无限循环,该循环将使用time.sleep(600)
类的东西每十分钟检查一次股价。 Note that you can add your program to run at startup fairly easily: 请注意,您可以添加程序以在启动时轻松运行:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.