Hans Meine wrote: > Am Sonntag, 05. Oktober 2008 22:01:15 schrieb Dirk Meyer: >> > I must confess that I never used this yield-syntax (or kaa notifier, >> > for that matter) before, so I may not fully understand what's going >> > on here.. >> >> The yield code is one of our best stuff :) >> http://doc.freevo.org/2.0/SourceDoc/Async > > I know, and I have repeatedly wrapped my head around coroutine concepts, but > this loop was a mystery for me at first: > for m in (yield kaa.beacon.query(type='media', media='ignore')): > > Now that I read it again, let me try to understand it: yield kaa.beacon.query > will actually suspend the surrounding function (coroutine), which is possible > only because this is in a main() function decorated as coroutine, and this > actually runs from kaa.main.run(), so the yield will yield an InProgress > object returned by beacon.query to the main loop, which will resume the > coroutine using python 2.5's new send() method, passing the result of the > InProgress object in for easy consumption by the above construct. > Is that about correct?
Yes. Read it as two statements: | result = yield kaa.beacon.query(type='media', media='ignore') | for m in result: The first statement goes back to main until the result arrives. In Python 2.5 you can just add (yield ...) around something that returns an InProgress. Let's asume you want to add f(x), g(x) and h(x) and f and h return InProgress objects because they take a bit longer (kaa.rpc, threaded, etc). The logic would be: | y = f(x) + g(x) + h(x) and all functions returning an InProgress have to be wrapped: | y = (yield f(x)) + g(x) + (yield h(x)) > That also means that the loop is actually over a "normal iterable"; Yes, yield "returns" the result of the InProgress. > Still, the asynchronously returned iterable may be itself > implemented as a coroutine, so without further knowledge/debugging > my suspicion that this keeps a db connection open may still be true? What do you mean? > 1) I did not understand this sentence: "Because of this idiom, in Python 2.5 > the yield statement will raise an exception if there is one while Python 2.4 > continues and raises the exception when calling get_result." > What does this want to tell me? This is not about kaa, but about Python, > right? What are the implications of this when using kaa.*? In Python 2.4 the yield code does not return anything. This is why you have to call get_result(). This will either return the value or raise the exception from the coroutine. You would think that the following code would work: | async = something() | yield async | try: | result = async.get_result() | except: | pass It does in Python 2.4, but in 2.5 the exception will also be raised in the yield and you we do not catch this. The code MUST be: | async = something() | try: | yield async | result = async.get_result() | except: | pass But I prefer to require Python 2.5 making this code much smaller: | try: | result = yield something() | except: | pass > 2) MainThreadCallback: Would it make sense to add the sentence "You could > also > decorate needs_to_be_called_from_main using @kaa.threaded("kaa.MAINTHREAD") > as mentioned in the previous section."? Wouldn't that be the preferred way > nowadays? Depends. If you have a function is is likely to be called from the MainThread but at one case you call it from a thread, MainThreadCallback is the way to go. If you want to be sure, use the decorator. Keep in mind that the decorator takes some more time. > BTW: In http://doc.freevo.org/2.0/SourceDoc/KaaBase, the coroutine > decorator is still referred to as yield_execution once. I will fix that later. BTW, I lied about fixing your media problems yesterday. Looks like it has to wait until the weekend. Dischi -- If a train station is a place where a train stops, what's a workstation? ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Freevo-devel mailing list Freevo-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/freevo-devel