简体   繁体   English

如何在Python中全天每十分钟执行一次程序

[英]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.

相关问题 如何在整个一周(白天和黑夜)每 5 分钟轻松且廉价地运行一个简单的 python 脚本? - How to easily and cheaply run a simply python script every 5 mins for a whole week - day and night? 将作业安排为仅在 Python 中的特定日期每 5 分钟运行一次 - Schedule a job to run every 5 mins only on a particular day in python 如何从Python脚本中执行整个Javascript程序 - How to execute an entire Javascript program from within a Python script 如何列出 Python 中每隔十个数字的列表 - How do I make a list of every other ten numbers in Python plot 如何在一个字典中每十个数据帧? Python Pandas - How plot every ten dataframes in a dictonary ? Python Pandas 如何让我的脚本在 Windows 上使用 python 每 30 分钟重复一次 - how to get my script to repeat every 30 mins with python on windows 如何使用 crontab 每 10 分钟 RPi 运行一次 python 脚本 - How to use crontab to run a python script every 10 mins RPi 如何对一整年的电子表格数据执行每天的功能 - How to perform a function for every day on spreadsheet data of an entire year Python yerr条每十个样本 - Python yerr bars every ten samples python 每 2 分钟运行一次脚本,但失败 - python run script every 2 mins, but failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM