Question on asyncio

pfranken85 at gmail.com pfranken85 at gmail.com
Sun Feb 22 12:47:31 EST 2015


Hello!

I am just trying to get familiar with asyncio. It seems to be a good thing, however, I am still having troubles and feel pretty puzzled although I think I got the point what async IO means. This is the problem I am trying to accomplish:

I have some functions which are reading values from hardware. If one of the values changes, I want a corresponding notification to the connected clients. The network part shouldn't be the problem. Here is what I got so far:

@asyncio.coroutine
def check():
  old_val = read_value_from_device()
  yield from asyncio.sleep(2)
  new_val = read_value_from_device()
  # we may have fluctuations, so we introduce a threshold
  if abs(new_val-old_val) > 0.05:
      return new_val
  else:
      return None
      
@asyncio.coroutine
def runner():
  while 1:
    new = yield from check()
    print(new)
      
loop = asyncio.get_event_loop()
loop.run_until_complete(update())


Is this the way one would accomplish this task? Or are there better ways? Should read_value_from_device() be a @coroutine as well? It may contain parts that take a while ... Of course, instead of print(new) I would add the corresponding calls for notifying the client about the update.

Thanks!



More information about the Python-list mailing list