-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jason Tackaberry wrote: | On Fri, 2008-02-15 at 19:03 +0100, Duncan Webb wrote: |> If I'm understanding this correctly, The problem with callbacks and |> coroutines is that nothing can be returned so it can be tricky to make |> the code do what you want. For example, when someone selects a menu | | Not exactly. | | @coroutine-decorated functions return an InProgress object when they are | invoked. When the underlying decorated function yields a value other | than kaa.NotFinished or an InProgress object (or if it raises an | exception), the InProgress object the call originally returned is | "finished" with that value (or exception). | | So you can return values through coroutines by yielding them. | | @kaa.coroutine() | def func(): | yield 42 | | def handle_result(result): | print "Async result:", result | | func().connect(handle_result) | | Coroutines return InProgress objects, and these are pretty flexible | objects, and they can carry return values or exceptions. They can be | yielded from other coroutines, causing the other coroutine to resume | once the InProgress it returned is finished. It can then fetch the | value from it using get_result(). They can be connected to, as in the | above example, since InProgress objects are also signals. InProgress | objects have an 'exception' attribute that is also a signal, and it is | emitted when the async function raises an exception. | | An example: | | @kaa.coroutine() | def do_something_with_func(): | try: | result = yield func() | except: | print "func() raised an exception!" | yield | assert(result == 42) | | That syntax works with Python 2.5. For python 2.4 compatible syntax: | | @kaa.coroutine() | def do_something_with_func() | inprogress = func() | yield inprogress | try: | result = inprogress.get_result() | except: | print "func() raised an exception!" | yield | assert(result == 42) | | Now, it's also possible to block inside do_something_with_func(): | | @kaa.coroutine() | def do_something_with_func() | try: | result = func().wait() | except: | print "func() raised an exception!" | yield | assert(result == 42) | | But as dischi said, it's better to yield the InProgress func() returns, | causing do_something_with_func() to automatically resume when func() | returns. | | Have you read http://doc.freevo.org/2.0/SourceDoc/Async ?
Many times.... Thanks for the examples but all they show me is that you can only yield from other coroutines and can call a callback, but what I can't figure out is how to pass a result (except in-progress objects) back from either a coroutine or a callback. I'm pretty sure that you can't do this. Here is a stupid example, where ping should return True if the recordserver is running. # -*- coding: iso-8859-1 -*- import kaa import kaa.rpc class Rec: ~ def __init__(self): ~ self.socket = ('127.0.0.1', 18002) ~ self.secret = 'secret1' ~ self.server = kaa.rpc.Client(self.socket, self.secret) ~ @kaa.coroutine() ~ def ping(self): ~ inprogress = self.server.rpc('ping') ~ yield inprogress ~ result = inprogress.get_result() def handle_result(result): ~ print "ping result:", result ~ return result if __name__ == '__main__': ~ rec = Rec() ~ f = rec.ping().connect(handle_result) ~ print 'f=%r' % f ~ p = rec.ping() ~ print 'p=%r' % p ~ r = p.get_result() ~ print 'r=%r' % r ~ kaa.main.run() # python rec.py f=<Callback for <function handle_result at 0xb767fca4>> p=<kaa.notifier.coroutine.CoroutineInProgress object at 0xb768266c> Traceback (most recent call last): ~ File "rec.py", line 31, in ? ~ r = p.get_result() ~ File "/usr/lib/python2.4/site-packages/kaa/notifier/async.py", line 279, in get_result ~ raise RuntimeError('operation not finished') RuntimeError: operation not finished If I put the get_result code in a coroutine then it should work, but I still have an inprogress object from this coroutine and I want the result outside a kaa.coroutine. If you see what I mean. But I'm really *not* happy that it is not possible to sync an rpc call at some time in the future, except with the wait() but it's use is frowned upon. BTW The name coroutine is much better than yield_function, it does make the concept clear. Cheers, Duncan -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHtg5HNi6l+Xvys44RAvdWAKCI6T0j1wf3CAi191ovUI9INZxWxQCfZe67 YpT1wX+dUXjSQ84CmUZwulc= =zHIC -----END PGP SIGNATURE----- ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Freevo-devel mailing list Freevo-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/freevo-devel