How to constantly update variable in Python and use it?

Viewed 24

I am trying to code a trading bot using Python. I have a list of yesterday's stock prices and a function which changes this list ( so I need to run it daily). I also have second function which I need to run every minute and which takes the list from above as an argument. How do I make this work so that both functions run periodically, but do not run first function each time I run the second function?

list_prices = [...]
def function1():
    global list_prices
    list_prices = ...
    return
def function2(list_prices):
    ...
    return   
1 Answers

I'm not sure I understood the question correctly but does this answer it? Running function2 every 60 sec, function1 every 24 hours

from time import time

last_run_function1 = 0
while True:
    now = time()
    time_delta = now - last_run_function1
    if time_delta >= 60 * 60 * 24:  # 60 sec * 60 min * 24 hours
        function1()
        last_run_function1 = time()
    function2(list_prices)
    time.sleep(60)
Related