async def test_retry(self): meta = {'max_retries': 3, 'retry_delay': 1} done = create_future() class CheckRetry: count = 1 message_id = None def __call__(self, _, event, task): if task.name == 'subtraction': if task.meta.get('from_task') == self.message_id: self.count += 1 if task.retry == 3: done.set_result(task) else: self.message_id = task.id check_retry = CheckRetry() await self.api.on_events('task', 'done', check_retry) try: task = await self.api.tasks.queue('subtraction', a=1, b='foo', delay=1, callback=False, meta_params=meta) self.assertEqual(task.status_string, 'QUEUED') check_retry.message_id = task.id task = await done self.assertEqual(check_retry.count, 3) self.assertEqual(task.status_string, 'FAILURE') finally: await self.api.remove_event_callback('task', 'done', check_retry)
def shutdown(self, wait=True): with self._shutdown_lock: self._shutdown = True self._put(None) if wait: self._waiter = create_future(self._loop) return self._waiter
def __iter__(self): while True: data = self.file.read(self.block) if not data: break future = create_future() future.set_result(data) yield future
def close(self, msg=None): """Return a Future which should be called back once the consumer is closed""" if not self.closing(): self._closing_waiter = create_future(self._loop) if msg: self.logger.warning(msg) self.tick() return self._closing_waiter
def _throttle(self, rw): self.logger.debug('Throttling %s', self._types[rw]) if rw: assert not self._throttle[rw] self._throttle[rw] = create_future(self.protocol._loop) else: self._throttle[rw] = True t = self.protocol._transport t.pause_reading()
def acquire(self, timeout=None): """Acquires the lock if in the unlocked state otherwise switch back to the parent coroutine. """ green = getcurrent() parent = green.parent if parent is None: raise MustBeInChildGreenlet('GreenLock.acquire in main greenlet') if self._local.locked: future = create_future(self._loop) self._queue.append(future) parent.switch(future) self._local.locked = green return self.locked()
def submit(self, func, *args, **kwargs): """Equivalent to ``func(*args, **kwargs)``. This method create a new task for function ``func`` and adds it to the queue. Return a :class:`~asyncio.Future` called back once the task has finished. """ with self._shutdown_lock: if self._shutdown: raise RuntimeError( 'cannot schedule new futures after shutdown') if self.in_green_worker: return wait(func(*args, **kwargs)) else: future = create_future(self._loop) self._put((future, func, args, kwargs)) return future
async def ssl_transport(loop, rawsock, connection, sslcontext, hostname): if hasattr(loop, '_make_legacy_ssl_transport'): # TODO: this is a hack because the create_connection does not work # with standard asyncio event loops waiter = create_future(loop) loop._make_legacy_ssl_transport(rawsock, connection, sslcontext, waiter, server_hostname=hostname) await waiter else: await loop.create_connection(lambda: connection, ssl=sslcontext, sock=rawsock, server_hostname=hostname)
async def _test_fail_subscribe(self): channels = self.channels() original, warning, critical = self._patch(channels, channels.pubsub, 'subscribe') await channels.connect() args, kw = await critical.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], 2) critical.end = create_future() args, kw = await critical.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], 2.25) channels.pubsub.subscribe = original args, kw = await warning.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], channels.status_channel)
async def _test_fail_subscribe(self): channels = self.channels() original, warning, critical = self._patch( channels, channels.pubsub, 'subscribe' ) await channels.connect() args, kw = await critical.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], 2) critical.end = create_future() args, kw = await critical.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], 2.25) channels.pubsub.subscribe = original args, kw = await warning.end self.assertEqual(len(args), 3) self.assertEqual(args[1], channels) self.assertEqual(args[2], channels.status_channel)
def __call__(self, actor=None): """Register this application with the (optional) calling ``actor``. If an ``actor`` is available (either via the function argument or via the :func:`~pulsar.async.actor.get_actor` function) it must be ``arbiter``, otherwise this call is no-op. If no actor is available, it means this application starts pulsar engine by creating the ``arbiter`` with its :ref:`global settings <setting-section-global-server-settings>` copied to the arbiter :class:`.Config` container. :return: the ``start`` one time event fired once this application has fired it. """ if actor is None: actor = get_actor() monitor = None if actor and actor.is_arbiter(): monitor = actor.get_actor(self.name) if monitor is None and (not actor or actor.is_arbiter()): self.cfg.on_start() self.logger = self.cfg.configured_logger() if not actor: actor = pulsar.arbiter(cfg=self.cfg.clone()) else: self.update_arbiter_params(actor) if not self.cfg.exc_id: self.cfg.set('exc_id', actor.cfg.exc_id) if self.on_config(actor) is not False: start = create_future(actor._loop) actor.bind_event('start', partial(self._add_monitor, start)) return start else: return elif monitor: raise ImproperlyConfigured('%s already started ' % monitor.name) else: raise ImproperlyConfigured('Cannot start application from %s' % actor)
def send(self, message): assert self._waiting is None self._waiting = d = create_future(self._loop) self._transport.sendto(to_bytes(message)+self.separator) return d
def async_func(loop, value): p = create_future(loop) loop.call_later(DELAY, p.set_result, value) return p
def __init__(self, wsgi, environ, start_response): self.wsgi = wsgi self.environ = environ self.start_response = start_response self.future = create_future()
def send(self, message): assert self._waiting is None self._waiting = d = create_future(self._loop) self._transport.sendto(to_bytes(message) + self.separator) return d
def __init__(self): self.end = create_future()
def AsyncResponseMiddleware(environ, resp): '''This is just for testing the asynchronous response middleware ''' future = create_future() future._loop.call_soon(future.set_result, resp) return future