I'm trying to learn python async module, and I have searched everywhere on the Internet including youtube pycon and various other videos, but i could not find a way to get variables from one async function (running forever) and to pass variable to other async function (running forever)
demo code:
async def one():
while True:
ltp += random.uniform(-1, 1)
return ltp
async def printer(ltp):
while True:
print(ltp)
As with any other Python code, the two coroutines can communicate using an object they both share, most typically
self
:The issue with the above code is that
one()
keeps producing values regardless of whether anyone is reading them. Also, there is no guarantee thattwo()
is not running faster thanone()
, in which case it will see the same value more than once. The solution to both problems is to communicate via a bounded queue: