def sleep(dt): """Public function to sleep some time. Example: yield tasklets.sleep(0.5) # Sleep for half a sec. """ fut = Future('sleep(%.3f)' % dt) eventloop.queue_call(dt, fut.set_result, None) return fut
def set_result(self, result): if self._done: raise RuntimeError('Result cannot be set twice.') self._result = result self._done = True _state.remove_pending(self) for callback, args, kwds in self._immediate_callbacks: callback(*args, **kwds) for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def set_exception(self, exc, tb=None): if not isinstance(exc, BaseException): raise TypeError('exc must be an Exception; received %r' % exc) if self._done: raise RuntimeError('Exception cannot be set twice.') self._exception = exc self._traceback = tb self._done = True _state.remove_pending(self, status='fail') for callback, args, kwds in self._immediate_callbacks: callback(*args, **kwds) for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def tasklet_wrapper(*args, **kwds): __ndb_debug__ = utils.func_info(func) fut = Future('tasklet %s' % utils.func_info(func)) fut._context = get_context() try: result = func(*args, **kwds) except (StopIteration, Return) as err: result = get_return_value(err) if _is_generator(result): ns = namespace_manager.get_namespace() ds_conn = datastore._GetConnection() _state.add_generator(result) eventloop.queue_call(None, fut._help_tasklet_along, ns, ds_conn, result) else: fut.set_result(result) return fut
def add_callback(self, callback, *args, **kwds): if self._done: eventloop.queue_call(None, callback, *args, **kwds) else: self._callbacks.append((callback, args, kwds))