Reputation: 5309
Is it safe to do this?
@ndb.tasklet
def foo():
# Note that I do not yield right here
future = some_key.get_async()
# Perform some other code
if some_condition:
# I need the get_async result here
entity = yield future
# I also need the get_async result here.
# If some_condition was true, I would be yielding this future
# for the second time. Is this ok?
entity = yield future
Please don't tell me that I could just yield
at the top of the function and use entity
in the rest of the code. My actual function is a little more elaborate than this, and I have a few conditional codepaths that may need to use entity
and I want to yield
at the latest moment possible to be able to perform my other code while the entity is being fetched in the background.
Upvotes: 2
Views: 168
Reputation: 2459
Yes, it's safe!
If you check the Future class in ndb, it will set result when done, and multiple yields or get_result will check if it's done and return the stored result.
google/appengine/ext/ndb/tasklets.py, line 324 in SDK 1.7.4
Upvotes: 2