Exemplo n.º 1
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
Exemplo n.º 2
0
 def __init__(self, wsgi, environ, start_response):
     self.wsgi = wsgi
     self.environ = environ
     self.connection = wsgi_request(self.environ).cache.connection
     self.debug = self.connection._loop.get_debug()
     self.start_response = start_response
     self.future = create_future()
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 5
0
 def __init__(self, wsgi, environ, start_response):
     self.wsgi = wsgi
     self.environ = environ
     self.connection = wsgi_request(self.environ).cache.connection
     self.debug = self.connection._loop.get_debug()
     self.start_response = start_response
     self.future = create_future()
Exemplo n.º 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
Exemplo n.º 7
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()
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
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)
Exemplo n.º 11
0
def wait_for_stop(test, aid, terminating=False):
    '''Wait for an actor to stop'''
    actor = arbiter()
    waiter = create_future(loop=actor._loop)

    def remove():
        test.assertEqual(actor.event('periodic_task').unbind(check), 1)
        waiter.set_result(None)

    def check(caller, **kw):
        test.assertEqual(caller, actor)
        if not terminating:
            test.assertFalse(aid in actor.managed_actors)
        elif aid in actor.managed_actors:
            return
        actor._loop.call_soon(remove)

    actor.event('periodic_task').bind(check)
    return waiter
Exemplo n.º 12
0
def wait_for_stop(test, aid, terminating=False):
    '''Wait for an actor to stop'''
    actor = arbiter()
    waiter = create_future(loop=actor._loop)

    def remove():
        test.assertEqual(actor.event('periodic_task').unbind(check), 1)
        waiter.set_result(None)

    def check(caller, **kw):
        test.assertEqual(caller, actor)
        if not terminating:
            test.assertFalse(aid in actor.managed_actors)
        elif aid in actor.managed_actors:
            return
        actor._loop.call_soon(remove)

    actor.event('periodic_task').bind(check)
    return waiter
Exemplo n.º 13
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)
Exemplo n.º 14
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
Exemplo n.º 15
0
 def post_async(self, request):
     future = create_future()
     future._loop.call_later(0.5, lambda: future.set_result(['Hello!']))
     return future
Exemplo n.º 16
0
def async_func(loop, value):
    p = create_future(loop)
    loop.call_later(DELAY, p.set_result, value)
    return p
Exemplo n.º 17
0
 def __init__(self):
     self.end = create_future()
Exemplo n.º 18
0
 def post_async(self, request):
     future = create_future()
     future._loop.call_later(0.5, lambda: future.set_result(['Hello!']))
     return future
Exemplo n.º 19
0
async def async_function(test):
    future = create_future()
    future._loop.call_later(1, future.set_result, True)
    result = await future
    test.assertEqual(result, True)
    return result
Exemplo n.º 20
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
Exemplo n.º 21
0
 def __init__(self):
     self.end = create_future()
Exemplo n.º 22
0
def async_func(loop, value):
    p = create_future(loop)
    loop.call_later(DELAY, p.set_result, value)
    return p
Exemplo n.º 23
0
async def async_function(test):
    future = create_future()
    future._loop.call_later(1, future.set_result, True)
    result = await future
    test.assertEqual(result, True)
    return result