-1

I am receiving data from a streaming API that I insert into a list (this list receives data for about 8 hours a day) then after a given period of time perform some calculations on then insert the results of those calculations into another list then wait for another period of time and perform calculations on that list to create a third list. What is the best way of doing this in Python? Pseudocode:

 FirstList():
 ''' Wait 15 minutes to create first list'''

SecondList():
'''Calculate results from first list and create a second list after given period of time'''

 ThirdList():
 ''' Calculate results from SecondList and create third list after a given period of time'''
rafaelc
  • 57,686
  • 15
  • 58
  • 82
namor
  • 97
  • 1
  • 3
  • 11

1 Answers1

0

You can use the Timer class from Python's Threading module.

Your program would look something like this:

from threading import Timer

def buildLists():
    global list1, list2, list3
    list1 = FirstList()
    # Calculate other two lists
    list2 = SecondList()
    list3 = ThirdList()

# Run buildLists after 15 minutes (900 seconds)
listTimer = Timer(900.0, buildLists)
listTimer.start()

The way this works, is by creating a Timer instance, which runs a function on completion.

It wasn't clear to me exactly what you meant by '''Calculate results from first list and create a second list after given period of time''', so that section may need to be modified to your particular needs, but the general method should work for you.

rp.beltran
  • 2,764
  • 3
  • 21
  • 29
  • Thanks for the idea @rp.beltran but, if I understand your code correctly, it will start building the first list after a 15 minute delay. I did not explain myself very well. Let me try again. I need to have list1 contain the values from the data stream FOR 15 minutes; list1 needs to contain a nested list of 15 minutes worth of data(e.g. list1[[15minsofdata], [2nd15minsofdata]...]) and never stop creating that list. From there I extract the necessary values from each nested list in list1 and insert those values into list2, creating another nested list. – namor Jun 19 '16 at 12:59
  • To sum up: After 45 minute delay, when len(list1) == 3, x = calculated results from 3 nested lists in list1 then list2.append(x), when len(list2) == 3, r = calculations from 3 lists in list2 then list3.append(r). Wait 15 mins calculate newest 3 lists in list1 and append them to list2. – namor Jun 19 '16 at 13:59