def set_result(self, result): assert not self._done self._result = result self._done = True _state.remove_pending(self) for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def set_result(self, result): assert not self._done self._result = result self._done = True logging.debug('_all_pending: remove successful %s', self) self._all_pending.remove(self) for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def testRun(self): record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.2, foo, 42) eventloop.queue_call(0.1, foo, arg='hello') eventloop.run() self.assertEqual(record, ['hello', 42])
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_exception(self, exc, tb=None): assert isinstance(exc, BaseException) assert not self._done self._exception = exc self._traceback = tb self._done = True _state.remove_pending(self, status='fail') for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def add(self, arg): fut = tasklets.Future('%s.add(%s)' % (self, arg)) if not self._todo: # Schedule the callback # We use the fact that regular tasklets are queued at time None, # which puts them at absolute time 0 (i.e. ASAP -- still on a # FIFO basis). Callbacks explicitly scheduled with a delay of 0 # are only run after all immediately runnable tasklets have run. eventloop.queue_call(0, self._autobatcher_callback) self._todo.append((fut, arg)) return fut
def testRunWithRpcs(self): record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.1, foo, 42) config = datastore_rpc.Configuration(on_completion=foo) rpc = self.conn.async_get(config, []) self.assertEqual(len(rpc.rpcs), 1) eventloop.queue_rpc(rpc) eventloop.run() self.assertEqual(record, [rpc.rpcs[0], 42]) self.assertEqual(rpc.state, 2) # TODO: Use apiproxy_rpc.RPC.FINISHING.
def set_exception(self, exc, tb=None): assert isinstance(exc, BaseException) assert not self._done self._exception = exc self._traceback = tb self._done = True if self in self._all_pending: logging.debug('_all_pending: remove failing %s', self) self._all_pending.remove(self) else: logging.debug('_all_pending: not found %s', self) for callback, args, kwds in self._callbacks: eventloop.queue_call(None, callback, *args, **kwds)
def testRunWithRpcs(self): apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() stub = datastore_file_stub.DatastoreFileStub('_', None) apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub) record = [] def foo(arg): record.append(arg) eventloop.queue_call(0.1, foo, 42) conn = datastore_rpc.Connection() config = datastore_rpc.Configuration(on_completion=foo) rpc = conn.async_get(config, []) self.assertEqual(len(rpc.rpcs), 1) eventloop.queue_rpc(rpc) eventloop.run() self.assertEqual(record, [rpc.rpcs[0], 42]) self.assertEqual(rpc.state, 2) # TODO: Use apiproxy_rpc.RPC.FINISHING.
def testQueueTasklet(self): def f(): return 1 def g(): return 2 def h(): return 3 t_before = time.time() eventloop.queue_call(1, f, 42, 'hello', a=1, b=2) eventloop.queue_call(3, h, c=3, d=4) eventloop.queue_call(2, g, 100, 'abc') t_after = time.time() self.assertEqual(len(self.ev.queue), 3) [(t1, f1, a1, k1), (t2, f2, a2, k2), (t3, f3, a3, k3)] = self.ev.queue self.assertTrue(t1 < t2) self.assertTrue(t2 < t3) self.assertTrue(abs(t1 - (t_before + 1)) < t_after - t_before) self.assertTrue(abs(t2 - (t_before + 2)) < t_after - t_before) self.assertTrue(abs(t3 - (t_before + 3)) < t_after - t_before) self.assertEqual(f1, f) self.assertEqual(f2, g) self.assertEqual(f3, h) self.assertEqual(a1, (42, 'hello')) self.assertEqual(a2, (100, 'abc')) self.assertEqual(a3, ()) self.assertEqual(k1, {'a': 1, 'b': 2}) self.assertEqual(k2, {}) self.assertEqual(k3, {'c': 3, 'd': 4})
def tasklet(func): # XXX Docstring @utils.wrapping(func) def tasklet_wrapper(*args, **kwds): # XXX Docstring # TODO: make most of this a public function so you can take a bare # generator and turn it into a tasklet dynamically. (Monocle has # this I believe.) # __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, err: # Just in case the function is not a generator but still uses # the "raise Return(...)" idiom, we'll extract the return value. result = get_return_value(err) if is_generator(result): eventloop.queue_call(None, fut._help_tasklet_along, 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))