user3425118
user3425118

Reputation: 23

Run the script every X minute

I want this script to run every minute...

import time
import urllib2
from time import sleep

stocksToPull = str('1101'),str('1102'),str('1201'),str('1216'),str('1227')
def pullData(stock):
        try:
        fileLine = stock+'.txt'
        urlToVisit = 'http://mis.tse.com.tw/data/'+stock+'.csv'
        sourceCode = urllib2.urlopen(urlToVisit).read()
        splitSource = sourceCode.split('\n')

        for everyLine in splitSource:
            splitLine = everyLine.split(',')
            if len(splitLine)==40:
                saveFile = open(fileLine,'a')
                everyLine = everyLine.replace('"','')
                lineToWrite = everyLine+'\n'
                saveFile.write(lineToWrite)
        print 'pulled',stock
        print 'sleeping.....'
    except Exception,e:
        print ',main loop',str(e)
for eachStock in stocksToPull:
    pullData(eachStock)
while True:
     starttime = time.time() 
     pullData(eachStock)
     endtime = time.time()-starttime
     sleep(65-endtime)

and I run this,it's work. It will gets the data of eachstock at the first time but after 1 minute later it just pull '1227''s data, it wont pull all of them. It looks like this:

pulled 1101
sleeping.....
pulled 1102
sleeping.....
pulled 1201
sleeping.....
pulled 1216
sleeping.....
pulled 1227
sleeping.....
pulled 1227
sleeping.....
pulled 1227
sleeping.....

How could solve this problem? I use python 2.7.

Upvotes: 1

Views: 442

Answers (1)

woot
woot

Reputation: 7606

I think you really mean to do this:

while True:
    starttime = time.time() 

    for eachStock in stocksToPull:
        pullData(eachStock)

    duration = time.time()-starttime
    sleep(65-duration)

I assume you are calculating the sleep time because you want it to run exactly 65 seconds apart, regardless of how long it took to pullData() for each stock. You can use APScheduler to do something like this with an interval schedule. They have nice decorators, too.

Upvotes: 4

Related Questions