On Thu, Jan 31, 2013 at 2:05 AM, Moises Belchin <[email protected]> wrote:
> Is there any significant benefit between code#1 and code#2 ?? In docs you
> can read code#2 run in parallel.
> https://developers.google.com/appengine/docs/python/ndb/async#parallel_queries_yield
>
>> @ndb.tasklet
>>
>> def get_cart_plus_offers(acct):
>>
>>   cart, offers = yield get_cart_async(acct), get_offers_async(acct)
>>
>>   raise ndb.Return((cart, offers))
>>
>> That yield x, y is important but easy to overlook. If that were two
>> separate yield statements, they would happen in series. But yielding a tuple
>> of tasklets is a parallel yield: the tasklets can run in parallel and the
>> yield waits for all of them to finish and returns the results. (In some
>> programming languages, this is known as a barrier.)
>
>
> In appstats there is no significant difference between both options or maybe
> I don't see it. It's possible I'm missing something.
>
> Could someone bring to me more light here ?!
>
> Thanks in advance and best regards.
>
> Code#1:
>
> @ndb.tasklet
> def get_data(e):
>   usr = yield e.user.get_async()
>   det = yield MyKind.query(ancestor = e.key).fetch_async()
>   raise ndb.Return((e, usr, det))
>
> Code#2:
>
> @ndb.tasklet
> def get_data_parallel(e):
>   usr, det = yield (e.user.get_async(), MyKind.query(ancestor =
> e.key).fetch_async())
>   raise ndb.Return((e, usr, det))

Are you looking at Appstats in the dev appserver? It does not give
results (in cases like this) that match production. The dev appserver
does not really execute RPCs in parallel (which is the same as
concurrently, here).

I promise you that in production the "yield f1, f2" form runs the
tasks represented by futures f1 and f2 concurrently (== in parallel).

I should also explain (again) that there is a huge difference between this:

f1 = foo_async()
f2 = bar_async()
yield f1, f2

vs.

yield foo_async()
yield bar_async()

the latter is equivalent to

f1 = foo_async()
yield f1
f2 = bar_async()
yield f2

Because the first future is yielded before the second is even created,
nothing runs concurrently (== in parallel) here. However, now compare
to this:

f1 = foo_async()
f2 = bar_async()
yield f1
yield f2

This runs both futures in parallel (== concurrently) even though they
are yielded separately! The reason is that when you yield *any*
future, *all* futures that exist at that point are allowed to run. But
futures that haven't been created yet can't run!

Hope this helps. It is important to "get" this. (Also that no future
runs until you yield something. Futures are buffered in the app's
memory until a yield forces all buffered futures out to the servers.)

-- 
--Guido van Rossum (python.org/~guido)

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-appengine?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to