title: asyncio polling in the background with infinite printing
body: <p>I would like to get a basic idea working in Python:</p>
  
  <p>Let's say I have a blocking call to a function that reads the current speed of a car (I use python-can for this, and wait for the corresponding message to appear on the bus).</p>
  
  <p>On the other hand, I have an infinite loop that prints as precisely as possible the speed of the car.</p>
  
  <p>What I would like is the value of the speed to be updated directly, so that the infinite loop can print it as soon as it changes.</p>
  
  <p>Right now I had three ideas:</p>
  
  <ul>
  <li>Call my <code>get_speed</code> function in the loop, and block. This works, but only if this is the only value I am willing to print (spoiler alert: it's not). If I want to print precisely the RPM too, I have to wait for the speed to be found, possibly missing multiple RPM value before.</li>
  <li>Create another "thread" (subprocess/multiprocessing/thread, whatever you call it) that updates a global variable. I know this works, but meh, I feel like I could have something smarter</li>
  <li>From what I saw in javascript, there is a way to ask a function for a result, and keep going until this result is found (this rings some CPU bells right now ahah). I kinda want something like this, which could make me do the following</li>
  </ul>
  
  
  
  <pre><code>speed = get_speed()
  rpm = get_rpm()
  
  while True:
          print("RPM: {0} - Speed: {1}".format(rpm, speed))
  </code></pre>
  
  <p>This would (in my fairy world) actually display something like this:</p>
  
  <pre><code>RPM: None - Speed: None  # Function haven't returned yet, waiting
  RPM: None - Speed: None  # Same here
  RPM: None - Speed: None  # Same here
  RPM: 300 - Speed: None   # get_rpm has returned
  RPM: 300 - Speed: None   # Nothing happened
  RPM: 303 - Speed: 0      # get_rpm and get_speed have returned
  RPM: 303 - Speed: 0
  RPM: 312 - Speed: 0      # etc.
  </code></pre>
  
  <p>Right now what I have is something like this, which doesn't work (at all)</p>
  
  <pre><code>#!/usr/bin/env python3
  
  import datetime
  import asyncio
  import random
  from time import sleep
  
  async def _can_get_speed():
      # My long function
      print ("In   can_get_speed")
      sleep(4)
      print ("Out  can_get_speed")
      r = random.randint(0, 10)
      print(r)
      return r
  
  async def can_get_speed():
      return await asyncio.gather(_can_get_speed())
  
  if __name__ == "__main__":
      loop = asyncio.get_event_loop()
      speed = 0
      speed = loop.call_soon(can_get_speed, loop)
      loop.run_forever()
  
  
      while True:
          print("Speed: {0}".format(speed))
  </code></pre>
  
  <p>My two questions are:</p>
  
  <ol>
  <li>Is this the most correct way to do it (--> asyncio, I mean), and
  if not what is ? </li>
  <li>If it is, can somebody help me understanding
  asyncio better :)</li>
  </ol>
  
  <p>Thanks in advance all!</p>