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
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]))
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
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)
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
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]])
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
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))
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))
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)
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)
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)
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)
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')
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)
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
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
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)
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)
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)
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)
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()
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)
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)
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)
def __call__(self, channel, message): if channel == self.task_done: maybe_async(self._be.task_done_callback(message))
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)
def _on(self, handler, frame): maybe_async(handler(self, frame.body), loop=self._loop)
def _shut_down(self, result, exc=None): maybe_async(self.handler.on_close(self))
def connection_made(self, connection): connection.timeout = 0 maybe_async(self.handler.on_open(self), self._loop)
def __init__(self, name, ws): self.name = name self.ws = ws maybe_async(self.on_init())