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).
- How should I implement a queue because I am failing at it?
- Is the code structure good enough to start implementing the function with the real code?
speed_foreverandrain_forevershould be methods of a class.speedandrainshould be attributes of the same class.