Using python coroutines to implement easy of use state machines
Guillaume Chereau
charlie at openmoko.org
Wed May 28 08:42:22 CEST 2008
On Tue, 2008-05-27 at 14:41 +0800, Guillaume Chereau wrote:
> Aah, I just finished doing a simple "proof of concept" library to show
> how it could work (server side). I am quite happy with it because it
> allows generators OR callbacks syntax (in fact the library just provides
> a wrapper to a callback mechanism), so it is very easy to use with the
> gobject callback functions.
>
> Beside, the code is very small ( but quite tricky ! )
>
> Here is the code :
> http://pastebin.ca/1030773
I did an update of the code, to make it works better :
http://pastebin.ca/1031797
* It can now properly handle exceptions.
* I changed the syntax a little bit to make it look similar to the
python thread module.
* I added close() method to stop a Tasklet before the end of its
generator.
* I added several examples
I tried to make the library very general, so it could be used with
server and client code. There are two ways to use a Tasklet :
Either by using it inside an other tasklet, with the yield expression,
either by calling its 'start' method with a callback function (and an
optional error callback function as well)
We need to note that when a generator yield a value, it will be always
used as a return value except if the value is a Tasklet instance, so we
cannot write something like this :
( response, error ) = yield( "+CFUN?" )
Instead it has to be like this :
( response, error ) = yield (send("+CFUN?"))
or even better :
try:
response = yield (at.send("+CFUN?"))
except at.Error, inst:
# error handling
Cheers,
- Charlie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.linuxtogo.org/pipermail/smartphones-standards/attachments/20080528/62c96b62/attachment.pgp>
More information about the smartphones-standards
mailing list