Пример #1
0
    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)
Пример #2
0
 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
Пример #3
0
 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
Пример #4
0
    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)
Пример #5
0
 def __iter__(self):
     while True:
         data = self.file.read(self.block)
         if not data:
             break
         future = create_future()
         future.set_result(data)
         yield future
Пример #6
0
 def __iter__(self):
     while True:
         data = self.file.read(self.block)
         if not data:
             break
         future = create_future()
         future.set_result(data)
         yield future
Пример #7
0
 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
Пример #8
0
 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()
Пример #9
0
 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
Пример #10
0
    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()
Пример #11
0
    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()
Пример #12
0
    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
Пример #13
0
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)
Пример #14
0
    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
Пример #15
0
 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)
Пример #16
0
 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)
Пример #17
0
    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)
Пример #18
0
    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)
Пример #19
0
 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
Пример #20
0
def async_func(loop, value):
    p = create_future(loop)
    loop.call_later(DELAY, p.set_result, value)
    return p
Пример #21
0
def async_func(loop, value):
    p = create_future(loop)
    loop.call_later(DELAY, p.set_result, value)
    return p
Пример #22
0
 def __init__(self, wsgi, environ, start_response):
     self.wsgi = wsgi
     self.environ = environ
     self.start_response = start_response
     self.future = create_future()
Пример #23
0
 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
Пример #24
0
 def __init__(self):
     self.end = create_future()
Пример #25
0
 def __init__(self):
     self.end = create_future()
Пример #26
0
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
Пример #27
0
 def __init__(self, wsgi, environ, start_response):
     self.wsgi = wsgi
     self.environ = environ
     self.start_response = start_response
     self.future = create_future()