Seve
Seve

Reputation: 47

asyncio sharing variables in queue

I am studying Asyncio and so far I managed to prepare the structure for what I need.

import asyncio
import random

async def speed_forever():
    while True:
        speed = random.randint(1,100)
        print("Speed mesuring ......", speed)
        await asyncio.sleep(1)

async def rain_forever():
    while True:
        rain = random.random()
        print("Rain mesuring .......", rain)
        await asyncio.sleep(0.1)


async def main():
    asyncio.ensure_future(speed_forever())  # fire and forget
    asyncio.ensure_future(rain_forever())  # fire and forget

    while True:
        print("*" * 40)
        print("Sending Data.....")    #Here I'd like to get access to rain and speed variable by using a queue
        print("*" * 40)
        await asyncio.sleep(5)
     

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


Now, I'd like to get access to rain and speed from main() so can do something with those variables (eg sending over the air).

  1. How should I implement a queue because I am failing at it?
  2. Is the code structure good enough to start implementing the function with the real code?

Upvotes: 2

Views: 2220

Answers (1)

Roy2012
Roy2012

Reputation: 12493

Here's a skeleton for passing data from the *_forever functions back to main over a couple of queues. I've marked the new code with '***'

import asyncio
import random

rain_q = asyncio.Queue()      # ***
speed_q = asyncio.Queue()     # ***

async def speed_forever():
    while True:
        speed = random.randint(1,100)
        print("Speed mesuring ......", speed)
        await speed_q.put(speed)                 # ***
        await asyncio.sleep(1)

async def rain_forever():
    while True:
        rain = random.random()
        print("Rain mesuring .......", rain)
        await rain_q.put(rain)                   # ***
        await asyncio.sleep(0.1)


async def main():
    asyncio.ensure_future(speed_forever())  # fire and forget
    asyncio.ensure_future(rain_forever())  # fire and forget

    rain = None       # *** 
    speed = None      # ***
    while True:
        print("*" * 40)

        # *** Read stuff from the queues. Here, I'm just using the latest
        # item in the queue - but one can do other things as well. 
        while not rain_q.empty(): 
            rain = await rain_q.get()
        
        while not speed_q.empty(): 
            speed = await speed_q.get()

        print(f"*** Last rain was {rain}")            
        print(f"*** Last speed was {speed}")
        print("Sending Data.....")    #Here I'd like to get access to rain and speed variable by using a queue
        print("*" * 40)
        await asyncio.sleep(5)

     

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Note that if all you're interested in is the last item in the queue, there are other ways to achieve this. One, for example, would be a global variable of type float with the latest number. I don't think that's the right way to do it in this case.

Upvotes: 1

Related Questions