so in python, I have a function foo that takes several seconds to execute and returns a value, I have another function bar that calls foo and then runs some code on the result, this is slowing down my code as it can take several seconds to execute foo.
def foo():
#do some processing
return 'something'
def bar():
x=foo()
#do some processing on x to generate y
return y
how do I make it so that bar uses the value of foo from last time it was called, and then runs foo in the background and saves that value for next time foo is called?
>Solution :
One of simple solution could be run foo in Thread and make it store result in some global variable.
x = None
def foo():
global x
# do some processing
x = "something"
foo() # call it first time to get first x result
def bar():
# y = x if you want to make sure it doesn't change durign bar run
t = Thread(target=foo)
t.start()
# you can use x here