Esempio n. 1
0
 def __iter__(self):
     request = self.request
     rpc = request['rpc']
     status_code = 200
     try:
         result = rpc.process(request)
     except Exception as e:
         result = as_failure(e)
     handler = rpc.handler
     result = maybe_async(result)
     while is_async(result):
         yield b''
         result = maybe_async(result)
     try:
         if is_failure(result):
             e = result.trace[1]
             status_code = getattr(e, 'status', 400)
             log_failure(result)
             result = handler.dumps(rpc.id, rpc.version, error=e)
         else:
             result = handler.dumps(rpc.id, rpc.version, result=result)
     except Exception as e:
         LOGGER.error('Could not serialize', exc_info=True)
         status_code = 500
         result = handler.dumps(rpc.id, rpc.version, error=e)
     content = to_bytes(result)
     response = WsgiResponse(status_code, content,
                             content_type=handler.content_type)
     for c in self.start(response):
         yield c
Esempio n. 2
0
 def testNestedhandle(self):
     handle = lambda value : reduce(lambda x,y: x+y, value)\
                  if isinstance(value, list) else value 
     d = MultiDeferred(handle_value=handle)
     d.append((a for a in range(1,11)))
     r = maybe_async(d.lock())
     self.assertFalse(is_async(r))
     self.assertEqual(r, [55])
     handle = lambda value: 'c'*value
     d = MultiDeferred(handle_value=handle)
     d.append((a for a in range(1,11)))
     r = maybe_async(d.lock())
     self.assertFalse(is_async(r))
     self.assertTrue(is_failure(r[0]))
Esempio n. 3
0
 def response_generator(self, response, wsgi_response):
     response = maybe_async(response)
     while is_async(response):
         yield b''
         response = maybe_async(response)
     stream_content = None
     if is_failure(response):
         wsgi_response.status_code = 500
     else:
         wsgi_response.status_code = response.status_code
         wsgi_response.headers.update(response.headers)
         stream_content = response.stream()
     wsgi_response.start()
     if stream_content:
         for content in stream_content:
             yield content
Esempio n. 4
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. 5
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. 6
0
    def render(self, request=None):
        '''A shortcut function for synchronously rendering a Content.
This is useful during testing. It is the synchronous equivalent of
:meth:`content`.'''
        value = maybe_async(self.content(request))
        if is_failure(value):
            value.throw()
        return value
Esempio n. 7
0
 def testNested(self):
     d = MultiDeferred()
     # add a generator
     d.append((a for a in range(1,11)))
     r = maybe_async(d.lock())
     self.assertTrue(d.locked)
     self.assertFalse(is_async(r))
     self.assertEqual(r, [[1,2,3,4,5,6,7,8,9,10]])
Esempio n. 8
0
    def render(self, request=None):
        '''A shortcut function for synchronously rendering a Content.
This is useful during testing. It is the synchronous equivalent of
:meth:`content`.'''
        value = maybe_async(self.content(request))
        if is_failure(value):
            value.throw()
        return value
Esempio n. 9
0
def stream_mapping(value, request=None):
    result = {}
    async = False
    for key, value in iteritems(value):
        if isinstance(value, AsyncString):
            value = value.content(request)
        value = maybe_async(value)
        async = async or isinstance(value, Deferred)
        result[key] = value
    return multi_async(result) if async else result
Esempio n. 10
0
def wait_for_body_middleware(environ, start_response=None):
    '''Use this middleware to wait for the full body.

    This middleware wait for the full body to be received before letting
    other middleware to be processed.

    Useful when using synchronous web-frameworks.
    '''
    if environ['wsgi.input']:
        return maybe_async(_wait_for_body_middleware(environ, start_response))
Esempio n. 11
0
def wait_for_body_middleware(environ, start_response=None):
    """Use this middleware to wait for the full body.

    This middleware wait for the full body to be received before letting
    other middleware to be processed.

    Useful when using synchronous web-frameworks.
    """
    if environ["wsgi.input"]:
        return maybe_async(_wait_for_body_middleware(environ, start_response))
Esempio n. 12
0
 def test_async_get(self):
     q = Queue()
     self.assertEqual(q.qsize(), 0)
     item = maybe_async(q.get())
     self.assertIsInstance(item , Deferred)
     result = yield q.put('Hello')
     self.assertEqual(result, None)
     self.assertTrue(item.done())
     self.assertEqual(item.result, 'Hello')
     self.assertEqual(q.qsize(), 0)
Esempio n. 13
0
def stream_mapping(value, request=None):
    result = {}
    async = False
    for key, value in iteritems(value):
        if isinstance(value, AsyncString):
            value = value.content(request)
        value = maybe_async(value)
        async = async or isinstance(value, Deferred)
        result[key] = value
    return multi_async(result) if async else result
Esempio n. 14
0
 def test_maxsize_callback(self):
     q = Queue(maxsize=2)
     self.assertEqual(q.maxsize, 2)
     yield self. async .assertEqual(q.put('hello'), None)
     yield self. async .assertEqual(q.put('ciao'), None)
     result = maybe_async(q.put('bla'))
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'hello')
     self.assertEqual(q.qsize(), 2)
Esempio n. 15
0
 def test_async_get(self):
     q = Queue()
     self.assertEqual(q.qsize(), 0)
     item = maybe_async(q.get())
     self.assertIsInstance(item, Deferred)
     result = yield q.put('Hello')
     self.assertEqual(result, None)
     self.assertTrue(item.done())
     self.assertEqual(item.result, 'Hello')
     self.assertEqual(q.qsize(), 0)
Esempio n. 16
0
 def test_maxsize_callback(self):
     q = Queue(maxsize=2)
     self.assertEqual(q.maxsize, 2)
     yield self.async.assertEqual(q.put('hello'), None)
     yield self.async.assertEqual(q.put('ciao'), None)
     result = maybe_async(q.put('bla'))
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'hello')
     self.assertEqual(q.qsize(), 2)
Esempio n. 17
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. 18
0
def wait_for_body_middleware(environ, start_response=None):
    '''Use this middleware to wait for the full body.

    This middleware wait for the full body to be received before letting
    other middleware to be processed.

    Useful when using synchronous web-frameworks.
    '''
    request = wsgi_request(environ)
    return maybe_async(request.data_and_files(),
                       get_result=False).add_callback(lambda s: None)
Esempio n. 19
0
def wait_for_body_middleware(environ, start_response=None):
    '''Use this middleware to wait for the full body.

    This middleware wait for the full body to be received before letting
    other middleware to be processed.

    Useful when using synchronous web-frameworks.
    '''
    request = wsgi_request(environ)
    return maybe_async(request.data_and_files(),
                       get_result=False).add_callback(lambda s: None)
Esempio n. 20
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. 21
0
def wsgi_iterator(gen, encoding=None):
    encoding = encoding or "utf-8"
    for data in gen:
        data = maybe_async(data)
        while is_async(data):
            yield b""
            data = maybe_async(data)
        if data is NOT_DONE:
            yield b""
        elif data is None:
            continue
        elif is_failure(data):
            log_failure(data)
        else:
            if isinstance(data, bytes):
                yield data
            elif isinstance(data, string_type):
                yield data.encode(encoding)
            else:
                for b in wsgi_iterator(data, encoding):
                    yield b
Esempio n. 22
0
 def as_frame(self, connection, body):
     '''Build a websocket server frame from body.'''
     body = maybe_async(body)
     if is_async(body):
         return body.addBoth(lambda b: self.as_frame(connection, b))
     if is_failure(body):
         # We have a failure. shut down connection
         body.log()
         body = Frame.close('Server error')
     elif not isinstance(body, Frame):
         # If the body is not a frame, build a final frame from it.
         body = Frame(body or '', version=connection.protocol.version,
                      final=True)
     return body.msg
Esempio n. 23
0
    def start(self, worker):
        '''Called by the :class:`pulsar.Worker` *worker* when the task
start its execution. If no timeout has occured the task will switch to
a ``STARTED`` :attr:`Task.status` and invoke the :meth:`on_start`
callback.'''
        job = registry[self.name]
        result = None
        try:
            if self.maybe_revoked():
                yield self.on_timeout(worker)
            else:
                self.status = STARTED
                self.time_start = datetime.now()
                yield self.on_start(worker)
                consumer = TaskConsumer(self, worker, job)
                result = maybe_async(job(consumer, *self.args, **self.kwargs))
                if is_async(result):
                    yield result
                    result = maybe_async(result)
        except Exception as e:
            result = as_failure(e)
        finally:
            yield self.finish(worker, result)
Esempio n. 24
0
 def test_put_timeout(self):
     q = Queue(maxsize=2)
     self.assertEqual(q.maxsize, 2)
     yield self.async.assertEqual(q.put('hello'), None)
     yield self.async.assertEqual(q.put('ciao'), None)
     yield self.async.assertRaises(Full, q.put, 'bla1', timeout=0.5)
     result = maybe_async(q.put('bla2'))
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'hello')
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'ciao')
     item = yield q.get()
     self.assertEqual(item, 'bla2')
     self.assertEqual(q.qsize(), 0)
Esempio n. 25
0
    def wait_for_task(self, task_id, timeout=None):
        '''Asynchronously wait for a task with ``task_id`` to have finished
its execution. It returns a :class:`pulsar.Deferred`.'''
        # make sure we are subscribed to the task_done channel
        def _():
            self.pubsub
            task = yield self.get_task(task_id)
            if task:
                if task.done():  # task done, simply return it
                    when_done = self.pop_callback(task.id)
                    if when_done:
                        when_done.callback(task)
                    yield task
                else:
                    yield self.get_callback(task_id)
        return maybe_async(_(), timeout=timeout, get_result=False)
Esempio n. 26
0
 def test_put_timeout(self):
     q = Queue(maxsize=2)
     self.assertEqual(q.maxsize, 2)
     yield self. async .assertEqual(q.put('hello'), None)
     yield self. async .assertEqual(q.put('ciao'), None)
     yield self. async .assertRaises(Full, q.put, 'bla1', timeout=0.5)
     result = maybe_async(q.put('bla2'))
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'hello')
     self.assertEqual(q.qsize(), 2)
     item = yield q.get()
     self.assertEqual(item, 'ciao')
     item = yield q.get()
     self.assertEqual(item, 'bla2')
     self.assertEqual(q.qsize(), 0)
Esempio n. 27
0
 def data_received(self, data):
     frame = self.parser.decode(data)
     while frame:
         if frame.is_close:
             try:
                 self.close_reason = parse_close(frame.body)
             finally:
                 self._connection.close()
             break
         if frame.is_message:
             maybe_async(self.handler.on_message(self, frame.body))
         elif frame.is_bytes:
             maybe_async(self.handler.on_bytes(self, frame.body))
         elif frame.is_ping:
             maybe_async(self.handler.on_ping(self, frame.body))
         elif frame.is_pong:
             maybe_async(self.handler.on_pong(self, frame.body))
         frame = self.parser.decode()
Esempio n. 28
0
    def wait_for_task(self, task_id, timeout=None):
        '''Asynchronously wait for a task with ``task_id`` to have finished
its execution. It returns a :class:`.Deferred`.'''

        # make sure we are subscribed to the task_done channel
        def _():
            self.pubsub
            task = yield self.get_task(task_id)
            if task:
                if task.done():  # task done, simply return it
                    when_done = self.pop_callback(task.id)
                    if when_done:
                        when_done.callback(task)
                    yield task
                else:
                    yield self.get_callback(task_id)

        return maybe_async(_(), timeout=timeout, get_result=False)
Esempio n. 29
0
    def run_job(self, jobname, targs=None, tkwargs=None, **meta_params):
        '''Create a new :ref:`task <apps-taskqueue-task>` which may or
may not be queued. This method returns a :class:`.Deferred` which
results in the :attr:`Task.id` created.
If ``jobname`` is not a valid :attr:`.Job.name`,
a ``TaskNotAvailable`` exception occurs.

:parameter jobname: the name of a :class:`.Job`
    registered with the :class:`.TaskQueue` application.
:parameter targs: optional tuple used for the positional arguments in the
    task callable.
:parameter tkwargs: optional dictionary used for the key-valued arguments
    in the task callable.
:parameter meta_params: Additional parameters to be passed to the :class:`Task`
    constructor (not its callable function).
:return: a :class:`.Deferred` resulting in a :attr:`Task.id`
    on success.'''
        c = self.create_task(jobname, targs, tkwargs, **meta_params)
        return maybe_async(c, get_result=False).add_callback(self.put_task)
Esempio n. 30
0
    def run_job(self, jobname, targs=None, tkwargs=None, **meta_params):
        '''Create a new :ref:`task <apps-taskqueue-task>` which may or
may not be queued. This method returns a :class:`pulsar.Deferred` which
results in the :attr:`Task.id` created.
If ``jobname`` is not a valid :attr:`pulsar.apps.tasks.models.Job.name`,
a ``TaskNotAvailable`` exception occurs.

:parameter jobname: the name of a :class:`pulsar.apps.tasks.models.Job`
    registered with the :class:`pulsar.apps.tasks.TaskQueue` application.
:parameter targs: optional tuple used for the positional arguments in the
    task callable.
:parameter tkwargs: optional dictionary used for the key-valued arguments
    in the task callable.
:parameter meta_params: Additional parameters to be passed to the :class:`Task`
    constructor (not its callable function).
:return: a :class:`pulsar.Deferred` resulting in a :attr:`Task.id`
    on success.'''
        c = self.create_task(jobname, targs, tkwargs, **meta_params)
        return maybe_async(c, get_result=False).add_callback(self.put_task)
Esempio n. 31
0
 def __call__(self, channel, message):
     if channel == self.task_done:
         maybe_async(self._be.task_done_callback(message))
Esempio n. 32
0
File: api.py Progetto: japaks/pulsar
 def test_maybe_async_get_result_false(self):
     a = maybe_async(3, get_result=False)
     self.assertTrue(isinstance(a, Deferred))
     self.assertTrue(a.done())
     self.assertEqual(a.result, 3)
Esempio n. 33
0
 def _on(self, handler, frame):
     maybe_async(handler(self, frame.body), loop=self._loop)
Esempio n. 34
0
 def _shut_down(self, result, exc=None):
     maybe_async(self.handler.on_close(self))
Esempio n. 35
0
 def connection_made(self, connection):
     connection.timeout = 0
     maybe_async(self.handler.on_open(self), self._loop)
Esempio n. 36
0
 def _on(self, handler, frame):
     maybe_async(handler(self, frame.body), loop=self._loop)
Esempio n. 37
0
 def _shut_down(self, result, exc=None):
     maybe_async(self.handler.on_close(self))
Esempio n. 38
0
 def test_maybe_async_get_result_false(self):
     a = maybe_async(3, get_result=False)
     self.assertTrue(isinstance(a, Deferred))
     self.assertTrue(a.done())
     self.assertEqual(a.result, 3)
Esempio n. 39
0
 def connection_made(self, connection):
     connection.timeout = 0
     maybe_async(self.handler.on_open(self), self._loop)
Esempio n. 40
0
 def __call__(self, channel, message):
     if channel == self.task_done:
         maybe_async(self._be.task_done_callback(message))
Esempio n. 41
0
 def __init__(self, name, ws):
     self.name = name
     self.ws = ws
     maybe_async(self.on_init())
Esempio n. 42
0
 def __init__(self, name, ws):
     self.name = name
     self.ws = ws
     maybe_async(self.on_init())