예제 #1
0
 def read(self, n=None):
     '''Read all content'''
     if self._streamed:
         return b''
     buffer = []
     for body in self:
         if isfuture(body):
             body = yield from body
         buffer.append(body)
     return b''.join(buffer)
예제 #2
0
파일: __init__.py 프로젝트: robgil/pulsar
 def _(*args, **kwargs):
     gr = PulsarGreenlet(callable)
     # switch to the new greenlet
     result = gr.switch(*args, **kwargs)
     # back to the parent
     while isfuture(result):
         # keep on switching back to the greenlet if we get a Future
         result = gr.switch((yield From(result)))
     # For some reason this line does not show in coverage reports
     # but it is covered!
     coroutine_return(result)    # prgma nocover
예제 #3
0
 def _async(self, callable, future, safe=False):
     while isfuture(future):
         kw = {}
         try:
             resp = yield future
         except Exception as exc:
             if not safe:
                 raise
             future = callable(exc=exc)
         else:
             future = callable(resp)
     coroutine_return(future)
예제 #4
0
파일: handlers.py 프로젝트: dejlek/pulsar
    def _async(self, environ, start_response):
        response = None
        try:
            for middleware in self.middleware:
                response = middleware(environ, start_response)
                if isfuture(response):
                    response = yield from response
                if response is not None:
                    break
            if response is None:
                raise Http404

        except Exception as exc:
            response = handle_wsgi_error(environ, exc)

        if isinstance(response, WsgiResponse) and not response.started:
            for middleware in self.response_middleware:
                response = middleware(environ, response)
                if isfuture(response):
                    response = yield from response
            response.start(start_response)
        return response
예제 #5
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 such as :django:`django <>`.
    '''
    def _wsgi_input(value):
        environ['wsgi.input'] = BytesIO(value)

    if environ['wsgi.input']:
        stream = environ['wsgi.input']
        chunk = stream.read()
        if isfuture(chunk):
            return chain_future(chunk, callback=_wsgi_input)
        else:
            _wsgi_input(chunk)
예제 #6
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 such as :django:`django <>`.
    '''
    def _wsgi_input(value):
        environ['wsgi.input'] = BytesIO(value)

    if environ['wsgi.input']:
        stream = environ['wsgi.input']
        chunk = stream.read()
        if isfuture(chunk):
            return chain_future(chunk, callback=_wsgi_input)
        else:
            _wsgi_input(chunk)
예제 #7
0
 def _run(self, runner, test, methodName, previous=None, add_err=True):
     __skip_traceback__ = True
     method = getattr(test, methodName, None)
     if method:
         # Check if a testfunction object is already available
         # Check the run_on_arbiter decorator for information
         tfunc = getattr(method, 'testfunction', None)
         # python 3.4
         expecting_failure = getattr(
             method, '__unittest_expecting_failure__', False)
         if tfunc is None:
             tfunc = TestFunction(method.__name__)
         exc = tfunc(test, test.cfg.test_timeout)
         if isfuture(exc):
             try:
                 exc = yield From(exc)
             except Exception as e:
                 exc = e
         if exc:
             add_err = False if previous else add_err
             previous = self.add_failure(test, runner, exc, add_err,
                                         expecting_failure)
     coroutine_return(previous)
예제 #8
0
파일: case.py 프로젝트: robgil/pulsar
 def _run(self, runner, test, methodName, previous=None, add_err=True):
     __skip_traceback__ = True
     method = getattr(test, methodName, None)
     if method:
         # Check if a testfunction object is already available
         # Check the run_on_arbiter decorator for information
         tfunc = getattr(method, 'testfunction', None)
         # python 3.4
         expecting_failure = getattr(method,
                                     '__unittest_expecting_failure__',
                                     False)
         if tfunc is None:
             tfunc = TestFunction(method.__name__)
         exc = tfunc(test, test.cfg.test_timeout)
         if isfuture(exc):
             try:
                 exc = yield From(exc)
             except Exception as e:
                 exc = e
         if exc:
             add_err = False if previous else add_err
             previous = self.add_failure(test, runner, exc, add_err,
                                         expecting_failure)
     coroutine_return(previous)
예제 #9
0
    def _response(self, environ):
        exc_info = None
        response = None
        done = False
        while not done:
            done = True
            try:
                if exc_info is None:
                    if 'SERVER_NAME' not in environ:
                        raise HttpException(status=400)
                    response = self.wsgi_callable(environ, self.start_response)
                else:
                    response = handle_wsgi_error(environ, exc_info)
                #
                if isfuture(response):
                    response = yield From(response)
                #
                if exc_info:
                    self.start_response(response.status,
                                        response.get_headers(), exc_info)
                #
                # Do the actual writing
                loop = self._loop
                start = loop.time()
                for chunk in response:
                    if isfuture(chunk):
                        chunk = yield From(chunk)
                        start = loop.time()
                    result = self.write(chunk)
                    if isfuture(result):
                        yield From(result)
                        start = loop.time()
                    else:
                        time_in_loop = loop.time() - start
                        if time_in_loop > MAX_TIME_IN_LOOP:
                            self.logger.debug(
                                'Released the event loop after %.3f seconds',
                                time_in_loop)
                            yield From(None)
                            start = loop.time()
                #
                # make sure we write headers and last chunk if needed
                self.write(b'', True)

            except IOError:  # client disconnected, end this connection
                self.finished()
            except Exception:
                if wsgi_request(environ).cache.handle_wsgi_error:
                    self.keep_alive = False
                    self.connection.close()
                    self.finished()
                else:
                    done = False
                    exc_info = sys.exc_info()
            else:
                if not self.keep_alive:
                    self.connection.close()
                self.finished()
            finally:
                if hasattr(response, 'close'):
                    try:
                        response.close()
                    except Exception:
                        self.logger.exception(
                            'Error while closing wsgi iterator')
예제 #10
0
파일: pool.py 프로젝트: robgil/pulsar
 def _green_task(self, task):
     # Run in the main greenlet of the evnet-loop thread
     greenlet = self._available.pop()
     result = greenlet.switch(task)
     while isfuture(result):
         result = greenlet.switch((yield From(result)))
예제 #11
0
    def _response(self, environ):
        exc_info = None
        response = None
        done = False
        alive = self.cfg.keep_alive or 15
        while not done:
            done = True
            try:
                if exc_info is None:
                    if (not environ.get('HTTP_HOST') and
                            environ['SERVER_PROTOCOL'] != 'HTTP/1.0'):
                        raise BadRequest
                    response = self.wsgi_callable(environ, self.start_response)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                else:
                    response = handle_wsgi_error(environ, exc_info)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                #
                if exc_info:
                    self.start_response(response.status,
                                        response.get_headers(), exc_info)
                #
                # Do the actual writing
                loop = self._loop
                start = loop.time()
                for chunk in response:
                    if isfuture(chunk):
                        chunk = yield from wait_for(chunk, alive)
                        start = loop.time()
                    result = self.write(chunk)
                    if isfuture(result):
                        yield from wait_for(result, alive)
                        start = loop.time()
                    else:
                        time_in_loop = loop.time() - start
                        if time_in_loop > MAX_TIME_IN_LOOP:
                            self.logger.debug(
                                'Released the event loop after %.3f seconds',
                                time_in_loop)
                            yield None
                            start = loop.time()
                #
                # make sure we write headers and last chunk if needed
                self.write(b'', True)

            # client disconnected, end this connection
            except (IOError, AbortWsgi):
                self.finished()
            except Exception:
                if wsgi_request(environ).cache.handle_wsgi_error:
                    self.keep_alive = False
                    self._write_headers()
                    self.connection.close()
                    self.finished()
                else:
                    done = False
                    exc_info = sys.exc_info()
            else:
                log_wsgi_info(self.logger.info, environ, self.status)
                self.finished()
                if not self.keep_alive:
                    self.logger.debug('No keep alive, closing connection %s',
                                      self.connection)
                    self.connection.close()
            finally:
                if hasattr(response, 'close'):
                    try:
                        response.close()
                    except Exception:
                        self.logger.exception(
                            'Error while closing wsgi iterator')
예제 #12
0
파일: server.py 프로젝트: luffyhwl/pulsar
    def _response(self, environ):
        exc_info = None
        response = None
        done = False
        alive = self.cfg.keep_alive or 15
        while not done:
            done = True
            try:
                if exc_info is None:
                    if "SERVER_NAME" not in environ:
                        raise HttpException(status=400)
                    response = self.wsgi_callable(environ, self.start_response)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                else:
                    response = handle_wsgi_error(environ, exc_info)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                #
                if exc_info:
                    self.start_response(response.status, response.get_headers(), exc_info)
                #
                # Do the actual writing
                loop = self._loop
                start = loop.time()
                for chunk in response:
                    if isfuture(chunk):
                        chunk = yield from wait_for(chunk, alive)
                        start = loop.time()
                    result = self.write(chunk)
                    if isfuture(result):
                        yield from wait_for(result, alive)
                        start = loop.time()
                    else:
                        time_in_loop = loop.time() - start
                        if time_in_loop > MAX_TIME_IN_LOOP:
                            self.logger.debug("Released the event loop after %.3f seconds", time_in_loop)
                            yield None
                            start = loop.time()
                #
                # make sure we write headers and last chunk if needed
                self.write(b"", True)

            except IOError:  # client disconnected, end this connection
                self.finished()
            except Exception:
                if wsgi_request(environ).cache.handle_wsgi_error:
                    self.keep_alive = False
                    self._write_headers()
                    self.connection.close()
                    self.finished()
                else:
                    done = False
                    exc_info = sys.exc_info()
            else:
                if not self.keep_alive:
                    self.connection.close()
                self.finished()
                log_wsgi_info(self.logger.info, environ, self.status)
            finally:
                if hasattr(response, "close"):
                    try:
                        response.close()
                    except Exception:
                        self.logger.exception("Error while closing wsgi iterator")
예제 #13
0
    def _response(self, environ):
        exc_info = None
        response = None
        done = False
        alive = self.cfg.keep_alive or 15
        while not done:
            done = True
            try:
                if exc_info is None:
                    if (not environ.get('HTTP_HOST')
                            and environ['SERVER_PROTOCOL'] != 'HTTP/1.0'):
                        raise BadRequest
                    response = self.wsgi_callable(environ, self.start_response)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                else:
                    response = handle_wsgi_error(environ, exc_info)
                    if isfuture(response):
                        response = yield from wait_for(response, alive)
                #
                if exc_info:
                    self.start_response(response.status,
                                        response.get_headers(), exc_info)
                #
                # Do the actual writing
                loop = self._loop
                start = loop.time()
                for chunk in response:
                    if isfuture(chunk):
                        chunk = yield from wait_for(chunk, alive)
                        start = loop.time()
                    result = self.write(chunk)
                    if isfuture(result):
                        yield from wait_for(result, alive)
                        start = loop.time()
                    else:
                        time_in_loop = loop.time() - start
                        if time_in_loop > MAX_TIME_IN_LOOP:
                            self.logger.debug(
                                'Released the event loop after %.3f seconds',
                                time_in_loop)
                            yield None
                            start = loop.time()
                #
                # make sure we write headers and last chunk if needed
                self.write(b'', True)

            except IOError:  # client disconnected, end this connection
                self.finished()
            except Exception:
                if wsgi_request(environ).cache.handle_wsgi_error:
                    self.keep_alive = False
                    self._write_headers()
                    self.connection.close()
                    self.finished()
                else:
                    done = False
                    exc_info = sys.exc_info()
            else:
                if not self.keep_alive:
                    self.connection.close()
                self.finished()
                log_wsgi_info(self.logger.info, environ, self.status)
            finally:
                if hasattr(response, 'close'):
                    try:
                        response.close()
                    except Exception:
                        self.logger.exception(
                            'Error while closing wsgi iterator')