akhil
akhil

Reputation: 19

How to return values from a scheduled function in python?

I am using Finnhub api to get some live data. I have 2 codes, one which will login the api every once in a day and second one will give the data every hour.

The first function will give details to the second function based on which it will fetch the data.

Here is my first function

def finnhub_login():
    finnhub_client = finnhub.Client(api_key="xxxxxxxxxxxx")
   
    symbols = df['SYMBOLS'].to_list()
    return symbols,finnhub_client

I would like to use the output i.e symbols and finnhub client to the second function

Here is the second function

def finnhub_api_call(symbols,finnhub_client):
    main_value = 0
    for i in symbols:
        
      data = finnhub_client.quote(['symbol'])
      main_value += data['dp']
      
    return main_value


schedule.every(1).day.do(finnhub_login)
schedule.every(1).hour.do(finnhub_api_call,symbols,finnhub_client)

while True:
    schedule.run_pending()
    time.sleep(1)

In the above code, how do I save the return values of 1st function and then use it for the second function ?

Upvotes: 1

Views: 350

Answers (1)

alparslan mimaroğlu
alparslan mimaroğlu

Reputation: 1480

you can wrap all of this in a class and use class variables. You can instantiate your class and use the functions from there. Everytime you run the first function class variables will change. Second function will use the class variables instead of function parameters.

class FinnhubCaller:
    def __init__(self):
        pass

    def finnhub_login(self):
        self.client = finnhub.Client(api_key="xxxxxxxxxxxx")
        self.symbols = df['SYMBOLS'].to_list()

    def finnhub_api_call(self):
        main_value = 0
        for i in self.symbols:
            data = self.client.quote(['symbol'])
            main_value += data['dp']
      
        return main_value

caller = FinnhubCaller()
schedule.every(1).day.do(caller.finnhub_login)
schedule.every(1).hour.do(caller.finnhub_api_call)

while True:
    schedule.run_pending()
    time.sleep(1)

Upvotes: 1

Related Questions