Search code examples
pythonfunctionreturninfinite-loop

Python return values from one function to another in infinite loop


Problem:

def tradingview(): is infite loop which should return two numbers

def tradingview():
        while True:           
                with open(filepath, 'r') as f:
                        count_var_short = f.read().count('Exit Short    Open')
                        print('Current shorts open:',count_var_short)                            
                with open(filepath, 'r') as f:
                        count_var_long = f.read().count('Exit Long    Open')
                        print('Current longs open:',count_var_long)                           
        return (count_var_short,count_var_long)


def target_balance(count_var_short,count_var_long):
        current_target = cur_price_VWAP['vwap'] * freebalance['BTC']['free']
        print ('current_target long',current_target * count_var_short)
        print ('current_target short',current_target * count_var_long)

if __name__ == '__main__':
        Thread(target = tradingview).start()
        Thread(target = target_balance(count_var_short,count_var_long)).start()

This code gives error

Thread(target = target_balance(count_var_short,count_var_long)).start()
NameError: name 'count_var_short' is not defined

===============================================

i want def tradingview(): return two numbers

count_var_short and count_var_long

and use them in function

def target_balance(count_var_short,count_var_long):

i know i should read how return works, but i will very thankful if someone help me on my own code because it is much more understandable for me.


Solution

  • Rather than a function with a single return value, you are looking to create a generator, which yields an expression, and then resumes execution.

    You can rewrite tradingview to be an infinite generator:

    def tradingview():
        while True:
            with open(filepath, 'r') as f:
                count_var_short = f.read().count('Exit Short    Open')
                print('Current shorts open:',count_var_short)                            
            with open(filepath, 'r') as f:
                count_var_long = f.read().count('Exit Long    Open')
                print('Current longs open:',count_var_long)                           
            yield count_var_short, count_var_long
    

    The yield expression inside the loop will "return" the values you want before resuming execution. It will also turn tradingview from a regular function into a generator function, which returns the generator when called.

    Now you can rewrite the code that calls target_balance to use the generator:

    if __name__ == '__main__':
        for count_var_short, count_var_long in tradingview():
            target_balance(count_var_short,count_var_long)
    

    No need for strange threading behavior: Python has already supplied the behavior you need with generators. I suggest you read up on them (and the yield keyword in general).