Esempio n. 1
0
 def test_fail_deferred1(self):
     a = Deferred()
     d1 = Deferred().add_callback(lambda r: a.callback(r+2))\
                    .add_errback(a.callback)
     d1.callback('bla')
     self.assertIsInstance(a.result.error, TypeError)
     self.assertIsInstance(d1.result.error, TypeError)
     mute_failure(self, a.result)
Esempio n. 2
0
 def test_then1(self):
     a = Deferred()
     d1 = Deferred()
     d2 = d1.then().add_callback(lambda r: a.callback(r + 2))
     d1.callback(1)
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
     self.assertEqual(d2.result, 3)
Esempio n. 3
0
 def test_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback(1)
     yield a
     self.assertEqual(a.result, 3)
     self.assertEqual(d1.result, 1)
Esempio n. 4
0
 def get_callback(self, task_id):
     with self.lock:
         callbacks = self.callbacks
         when_done = callbacks.get(task_id)
         if not when_done:
             # No deferred, create one and check again
             callbacks[task_id] = when_done = Deferred()
         return when_done
Esempio n. 5
0
    def close(self):
        '''Stop listening for messages.

        :meth:`unsubscribe` from all :attr:`channels` and :attr:`patterns`
        and close the subscriber connection with redis.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._close, d)
        return d
Esempio n. 6
0
    def unsubscribe(self, *channels):
        '''Un-subscribe from a list of ``channels`` or ``channel patterns``.

        It returns an asynchronous component which results in the number of
        channels this handler is subscribed to.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._unsubscribe, channels, d)
        return d
Esempio n. 7
0
 def test_json_with_async_string2(self):
     d = Deferred()
     astr = wsgi.AsyncString(d)
     response = wsgi.Json({'bla': astr})
     self.assertEqual(len(response.children), 1)
     result = response.render()
     self.assertIsInstance(result, Deferred)
     d.callback('ciao')
     result = yield result
     self.assertEqual(result, json.dumps({'bla': 'ciao'}))
Esempio n. 8
0
 def test_fail_coroutine1(self):
     d1 = Deferred()
     a = maybe_async(c_summation(d1))
     d1.callback('bla')
     try:
         yield a
     except TypeError:
         pass
     else:
         raise TypeError
     self.assertEqual(d1.result, 'bla')
Esempio n. 9
0
 def _close(self, future):
     if self._connection:
         d = Deferred()
         exc_info = None
         try:
             yield self._unsubscribe((), d)
             yield d
         except Exception:
             exc_info = sys.exc_info()
         self._connection.close()
         self._reset()
         future.callback(exc_info)
Esempio n. 10
0
 def _execute(self, command, *args):
     if not self._connection:
         client = self.client()
         pool = client.connection_pool
         consumer = yield pool.request(client,
                                       'ping', (),
                                       release_connection=False).on_finished
         self.parser = consumer._request.parser
         connection = consumer.connection
         connection.set_consumer(None)
         connection.set_consumer(self)
         self.fire_event('pre_request')
     d = Deferred()
     yield self._connection_msg(command, args, d)
Esempio n. 11
0
    def subscribe(self, *channels):
        '''Subscribe to a list of ``channels`` or ``channel patterns``.

        It returns an asynchronous component which results in the number of
        channels this handler is subscribed to.

        If this is the first time the method is called by this handler,
        than the :class:`PubSub` starts listening for messages which
        are fired via the ``on_message`` event.
        '''
        d = Deferred()
        loop = self.connection_pool.get_event_loop()
        loop.call_soon_threadsafe(self._subscribe, channels, d)
        return d
Esempio n. 12
0
def async_func(loop, value):
    p = Deferred()
    loop.call_later(0.01, p.callback, value)
    return p
Esempio n. 13
0
 def test_coroutine(self):
     loop = new_event_loop(iothreadloop=False)
     d = Deferred()
     loop.call_soon(main, d, loop, 1)
     loop.run_until_complete(d)
     self.assertEqual(d.result, 9)
Esempio n. 14
0
 def __init__(self, headers, parser, transport=None):
     self.headers = headers
     self.parser = parser
     self.transport = transport
     self.buffer = b''
     self.on_message_complete = Deferred()