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.
I need a solution which works on Windows.
Asked
Active
Viewed 1.0k times
4

ElmoVanKielmo
- 10,907
- 2
- 32
- 46

Shiva Prakash
- 1,849
- 4
- 21
- 25
-
1Add it to as a cronjob. – badc0re Mar 05 '15 at 12:35
-
@badc0re say suppose i have to print 'hi' every secs for one min. so def f(): print('hi'). how do I run this function ? – Shiva Prakash Mar 05 '15 at 12:37
-
Yeah best would be to run this script as cronjob.. – planet260 Mar 05 '15 at 12:39
-
1@badc0re how about in Windows – Shiva Prakash Mar 05 '15 at 12:42
-
1@Shiva, You may look at a scheduler in windows which is similar to cron in linux. It runs periodically at whatever the time interval you set it.http://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows – J Bourne Mar 05 '15 at 13:11
4 Answers
6
Ok, so you have a python function f() which you want to execute every ten mins for a day. 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)

francisco sollima
- 7,952
- 4
- 22
- 38
-
3This answer ignores the running time of f(). If f() takes 1 min to run, it will be executed only every 11 min. – horns Mar 05 '15 at 13:19
-
I didn't understand that from the question, but indeed, my answer executes f() every ten minutes plus the execution time. – francisco sollima Mar 05 '15 at 13:23
-
@francisco I am not getting the output but there are no errors too :( – Shiva Prakash Mar 05 '15 at 13:24
-
Can you show the code of the function? Or at least give more details as to what it outputs. – francisco sollima Mar 05 '15 at 13:40
1
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

Mohamed Ramzy Helmy
- 169
- 8
1
Use crontab. Open the crontab file via terminal:
$ crontab -e
At the end of the file you can set the python script and the frequency of running it. For example to run a script every 10 mins:
*/10 * * * * /path/to/script

barnabas markus
- 61
- 7
-
2
-
time.sleep(10*60) works also, but not suggested. if you restart your computer it will not start only if you run it manually. – barnabas markus Mar 05 '15 at 12:42
-
@ barney let me day I have a function 'f'. how do I execute it after every ten seconds. Can you please let me know the code ? – Shiva Prakash Mar 05 '15 at 12:44
-
-
Note that this solution is limited to a script runtime smaller than 10 minutes because the scheduler does not know about job completion. – Michael Jaros Mar 05 '15 at 13:28
-
i think if the runtime of the script is longer then 10mins, then the crontab will start a new process and let the previous script run till it ends. – barnabas markus Mar 05 '15 at 13:53
1
Windows has a Task Scheduler built-in. 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)
. Note that you can add your program to run at startup fairly easily:

Community
- 1
- 1

Mike Driscoll
- 32,629
- 8
- 45
- 88