Hi Jim,

thanks for the answer and for the code.

I think your code is a better aproach for paralleling yields and for check
if one of them is None.

Thanks in advance and regards.


Saludos.
Moisés Belchín.


2013/1/30 Jim Morrison <[email protected]>

> We added the following method:
> @staticmethod
>     @ndb.tasklet
>     def get_key_async(key):
>         """
>         returns a future that upon calling get_result() will either
> return the Future's result
>          or None if the key was None
>         """
>         if not key: raise ndb.Return(None)
>         result = yield key.get_async()
>         raise ndb.Return(result)
>
> to our code, a similar tasklet could be (which we've removed from out
> code):
> @ndb.tasklet
> def future_or_none(future):
>     if future:
>         result = yield future
>         raise ndb.Return(result)
>     raise ndb.Return(None)
>
> Then in your case you'd do
> yield (future_or_none(e.user.get_async() if e.user else None), ...)
>
>
>
>
> On Wed, Jan 30, 2013 at 10:03 AM, Guido van Rossum <[email protected]>
> wrote:
> > On Wed, Jan 30, 2013 at 9:00 AM, Moises Belchin <[email protected]>
> wrote:
> >> Please take a look at this parallel tasklet code snippet #1.
> >>
> >>
> >> @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))
> >>
> >>
> >>
> >> If e.user is None this raise an Exception.
> >>
> >>
> >> I'm trying this snippet #2. However I still get Exception: "TypeError:
> >> Expected Future, received <type 'NoneType'>: None"
> >>
> >>
> >> @ndb.tasklet
> >> def get_data_parallel(e):
> >>   usr, det = yield (e.user.get_async() if e.user else None,
> >>                     MyKind.query(ancestor = e.key).fetch_async())
> >>   raise ndb.Return((e, usr, det))
> >>
> >>
> >> How can I do something like snippet #2 ? How can I return future(None)
> or
> >> future('')
> >
> > You can factor it out into two yields, one of which is optional. First
> > create a future for the query that you always want to run:
> >
> > f = MyKind.query(ancestor = e.key).fetch_async()  # No yield!
> >
> > Then conditionally yield the other async request:
> >
> > if e.user:
> >   usr = yield from e.user.get_async()
> > else:
> >   usr = None
> >
> > Finally yield the future:
> >
> > det = yield f
> >
> > The trick is that the query will run when you yield the other operation.
> >
> > --
> > --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.
> >
> >
>
>
>
> --
> Jim
>
> --
> 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.
>
>
>

-- 
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