本文整理匯總了Python中google.appengine.ext.ndb.tasklet方法的典型用法代碼示例。如果您正苦於以下問題:Python ndb.tasklet方法的具體用法?Python ndb.tasklet怎麽用?Python ndb.tasklet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類google.appengine.ext.ndb
的用法示例。
在下文中一共展示了ndb.tasklet方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _check_etag
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def _check_etag(self, etag):
"""Check if etag is the same across requests to GCS.
If self._etag is None, set it. If etag is set, check that the new
etag equals the old one.
In the __init__ method, we fire one HEAD and one GET request using
ndb tasklet. One of them would return first and set the first value.
Args:
etag: etag from a GCS HTTP response. None if etag is not part of the
response header. It could be None for example in the case of GCS
composite file.
Raises:
ValueError: if two etags are not equal.
"""
if etag is None:
return
elif self._etag is None:
self._etag = etag
elif self._etag != etag:
raise ValueError('File on GCS has changed while reading.')
示例2: _run_until_rpc
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def _run_until_rpc():
"""Eagerly evaluate tasklets until it is blocking on some RPC.
Usually ndb eventloop el isn't run until some code calls future.get_result().
When an async tasklet is called, the tasklet wrapper evaluates the tasklet
code into a generator, enqueues a callback _help_tasklet_along onto
the el.current queue, and returns a future.
_help_tasklet_along, when called by the el, will
get one yielded value from the generator. If the value if another future,
set up a callback _on_future_complete to invoke _help_tasklet_along
when the dependent future fulfills. If the value if a RPC, set up a
callback _on_rpc_complete to invoke _help_tasklet_along when the RPC fulfills.
Thus _help_tasklet_along drills down
the chain of futures until some future is blocked by RPC. El runs
all callbacks and constantly check pending RPC status.
"""
el = eventloop.get_event_loop()
while el.current:
el.run0()
示例3: test_cache_with_tasklets
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def test_cache_with_tasklets(self):
@utils.cache
def f():
ndb.sleep(0).wait() # Yield thread.
return 1
@ndb.tasklet
def g():
yield () # Make g a generator.
raise ndb.Return(f())
def test():
ndb.Future.wait_all([(g()), (g())])
t = threading.Thread(target=test)
t.daemon = True
t.start()
t.join(1)
if t.is_alive():
self.fail('deadlock')
示例4: mock_methods
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def mock_methods(self):
calls = []
@ndb.tasklet
def via_jwt(*args):
calls.append(('jwt_based', args))
raise ndb.Return({'access_token': 'token', 'exp_ts': 0})
self.mock(service_account, '_mint_jwt_based_token_async', via_jwt)
def via_gae_api(*args):
calls.append(('gae_api', args))
return 'token', 0
self.mock(service_account.app_identity, 'get_access_token', via_gae_api)
return calls
示例5: setUp
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def setUp(self):
super(NetTest, self).setUp()
@ndb.tasklet
def get_access_token(*_args):
raise ndb.Return(('own-token', 0))
self.mock(auth, 'get_access_token_async', get_access_token)
@ndb.tasklet
def get_project_access_token(project_id, _scopes):
raise ndb.Return(('%s-token' % project_id, 0))
self.mock(auth, 'get_project_access_token_async', get_project_access_token)
self.mock(
auth, 'is_internal_domain',
lambda domain: domain == 'internal.example.com')
self.mock(logging, 'warning', lambda *_args: None)
self.mock(logging, 'error', lambda *_args: None)
示例6: test_project_tokens_fallback
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def test_project_tokens_fallback(self):
@ndb.tasklet
def get_project_access_token(_project_id, _scopes):
raise auth.NotFoundError('not found')
self.mock(auth, 'get_project_access_token_async', get_project_access_token)
self.mock_urlfetch([
({
'deadline': 10,
'headers': {
# Switches to own token.
'Authorization': 'Bearer own-token',
},
'method': 'GET',
'url': 'https://external.example.com/123',
}, Response(200, '', {})),
])
net.request(url='https://external.example.com/123', project_id='project-id')
示例7: mock_memcache
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def mock_memcache(self):
local_mc = {'store': {}, 'reads': 0, 'writes': 0}
@ndb.tasklet
def mock_memcache_get(version, desc, part=None):
value = local_mc['store'].get(bot_code.bot_key(version, desc, part))
if value is not None:
local_mc['reads'] += 1
raise ndb.Return(value)
@ndb.tasklet
def mock_memcache_set(value, version, desc, part=None):
local_mc['writes'] += 1
key = bot_code.bot_key(version, desc, part)
local_mc['store'][key] = value
return ndb.Return(None)
self.mock(bot_code, 'bot_memcache_set', mock_memcache_set)
self.mock(bot_code, 'bot_memcache_get', mock_memcache_get)
self.mock(bot_code, 'MAX_MEMCACHED_SIZE_BYTES', 100000)
return local_mc
示例8: _remove_old_entity_async
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def _remove_old_entity_async(key, now):
"""Removes a stale TaskDimensions or BotTaskDimensions instance.
Returns:
key if it was deleted.
"""
obj = yield key.get_async()
if not obj or obj.valid_until_ts >= now:
raise ndb.Return(None)
@ndb.tasklet
def tx():
obj = yield key.get_async()
if obj and obj.valid_until_ts < now:
yield key.delete_async()
raise ndb.Return(key)
res = yield datastore_utils.transaction_async(
tx, propagation=ndb.TransactionOptions.INDEPENDENT)
raise ndb.Return(res)
示例9: test_unordered_first_concurrent_jobs
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def test_unordered_first_concurrent_jobs(self):
items = [10, 5, 0]
log = []
@ndb.tasklet
def fn_async(x):
log.append('%d started' % x)
yield ndb.sleep(float(x) / 1000)
log.append('%d finishing' % x)
raise ndb.Return(x)
expected = [(5, 5), (0, 0), (10, 10)]
actual = utils.async_apply(
items, fn_async, concurrent_jobs=2, unordered=True)
self.assertFalse(isinstance(actual, list))
self.assertEqual(expected, list(actual))
self.assertEqual(log, [
'10 started',
'5 started',
'5 finishing',
'0 started',
'0 finishing',
'10 finishing',
])
示例10: _get_project_configs_async
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def _get_project_configs_async(project_ids, path, message_factory):
"""Returns a mapping {project_id: message}.
If a project does not exist, the message is None.
"""
assert isinstance(project_ids, list)
if not project_ids:
empty = ndb.Future()
empty.set_result({})
return empty
@ndb.tasklet
def get_async():
prefix = 'projects/'
msgs = yield storage.get_latest_messages_async(
[prefix + pid for pid in _filter_existing(project_ids)],
path, message_factory)
raise ndb.Return({
# msgs may not have a key because we filter project ids by existence
pid: msgs.get(prefix + pid)
for pid in project_ids
})
return get_async()
示例11: urlfetch_async
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def urlfetch_async(self, url, **kwds):
"""Make an async urlfetch() call.
This just passes the url and keyword arguments to NDB's async
urlfetch() wrapper in the current context.
This returns a Future despite not being decorated with @ndb.tasklet!
"""
ctx = ndb.get_context()
return ctx.urlfetch(url, **kwds)
示例12: __init__
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def __init__(self,
retry_params,
retriable_exceptions=_RETRIABLE_EXCEPTIONS,
should_retry=lambda r: False):
"""Init.
Args:
retry_params: an RetryParams instance.
retriable_exceptions: a list of exception classes that are retriable.
should_retry: a function that takes a result from the tasklet and returns
a boolean. True if the result should be retried.
"""
self.retry_params = retry_params
self.retriable_exceptions = retriable_exceptions
self.should_retry = should_retry
示例13: _eager_tasklet
# 需要導入模塊: from google.appengine.ext import ndb [as 別名]
# 或者: from google.appengine.ext.ndb import tasklet [as 別名]
def _eager_tasklet(tasklet):
"""Decorator to turn tasklet to run eagerly."""
@utils.wrapping(tasklet)
def eager_wrapper(*args, **kwds):
fut = tasklet(*args, **kwds)
_run_until_rpc()
return fut
return eager_wrapper