当前位置: 首页>>代码示例>>Python>>正文


Python ndb.tasklet方法代码示例

本文整理汇总了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.') 
开发者ID:googlearchive,项目名称:billing-export-python,代码行数:25,代码来源:storage_api.py

示例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() 
开发者ID:einaregilsson,项目名称:MyLife,代码行数:23,代码来源:api_utils.py

示例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') 
开发者ID:luci,项目名称:luci-py,代码行数:22,代码来源:utils_test.py

示例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 
开发者ID:luci,项目名称:luci-py,代码行数:18,代码来源:service_account_test.py

示例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) 
开发者ID:luci,项目名称:luci-py,代码行数:21,代码来源:net_test.py

示例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') 
开发者ID:luci,项目名称:luci-py,代码行数:20,代码来源:net_test.py

示例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 
开发者ID:luci,项目名称:luci-py,代码行数:24,代码来源:bot_code_test.py

示例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) 
开发者ID:luci,项目名称:luci-py,代码行数:22,代码来源:task_queues.py

示例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',
    ]) 
开发者ID:luci,项目名称:luci-py,代码行数:27,代码来源:utils_test.py

示例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() 
开发者ID:luci,项目名称:luci-py,代码行数:26,代码来源:projects.py

示例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) 
开发者ID:googlearchive,项目名称:billing-export-python,代码行数:12,代码来源:rest_api.py

示例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 
开发者ID:einaregilsson,项目名称:MyLife,代码行数:17,代码来源:api_utils.py

示例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 
开发者ID:einaregilsson,项目名称:MyLife,代码行数:12,代码来源:api_utils.py


注:本文中的google.appengine.ext.ndb.tasklet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。