Question on asyncio

pfranken85 at gmail.com pfranken85 at gmail.com
Mon Feb 23 04:48:15 EST 2015


Hello Marko!

Am Sonntag, 22. Februar 2015 22:21:55 UTC+1 schrieb Marko Rauhamaa:
> In asyncio, you typically ignore the value returned by yield. While
> generators use yield to communicate results to the calling program,
> coroutines use yield only as a "trick" to implement cooperative
> multitasking and an illusion of multithreading.

Really? I saw several exmaples, where a coroutine returned a value which was then picked up from the yield from statement...

> 
> Thus, "yield from" in asyncio should be read, "this is a blocking
> state."
> 
> > 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.
> 
> How do you read a value from the hardware? Do you use a C extension? Do
> you want read_value_from_device() to block until the hardware has the
> value available or is the value always available for instantaneous
> reading?
> 
> If the value is available instantaneously, you don't need to turn it
> into a coroutine. However, if blocking is involved, you definitely
> should do that. Depending on your hardware API it can be easy or
> difficult. If you are running CPython over linux, hardware access
> probably is abstracted over a file descriptor and a coroutine interface
> would be simple.


The corresponding call is a call to the python smbus library. It includes several sleeps (even though they are only about 50ms). Therefore I think it is worthwhile to encapsulate it into a coroutine. However I am not quite sure, how I should call it and integrate it into the main loop. In particular, do I need something like the runner routine() with and infinite while loop or can just add the check() routine to the main loop, so that it gets executed regularly and call the corresponding notifier whenever the condition of the if statement is true?

Thanks for your input!




More information about the Python-list mailing list