示例#1
0
 def start():
     pid = os.getpid()
     try:
         logger.info("Starting worker [%s]", pid)
         if constants.is_py37():
             loop.run_until_complete(http_server.serve_forever())
         else:
             loop.run_forever()
     finally:
         http_server.close()
         loop.run_until_complete(http_server.wait_closed())
         loop.run_until_complete(loop.shutdown_asyncgens())
         loop.close()
示例#2
0
def serve(
    request_handler,
    error_handler,
    debug=False,
    request_timeout=60,
    response_timeout=60,
    keep_alive_timeout=75,
    ssl=None,
    sock=None,
    request_max_size=100000000,
    reuse_port=False,
    loop=None,
    protocol=HttpProtocol,
    backlog=100,
    register_sys_signals=True,
    run_multiple=False,
    run_async=False,
    connections=None,
    signal=Signal(),
    request_class=None,
    access_log=True,
    keep_alive=True,
    is_request_stream=False,
    router=None,
    websocket_max_size=None,
    websocket_max_queue=None,
    websocket_read_limit=2 ** 16,
    websocket_write_limit=2 ** 16,
    state=None,
):
    """Start asynchronous HTTP Server on an individual process.

    :param request_handler: Sanic request handler with middleware
    :param error_handler: Sanic error handler with middleware
    :param debug: enables debug output (slows server)
    :param request_timeout: time in seconds
    :param response_timeout: time in seconds
    :param keep_alive_timeout: time in seconds
    :param ssl: SSLContext
    :param sock: Socket for the server to accept connections from
    :param request_max_size: size in bytes, `None` for no limit
    :param reuse_port: `True` for multiple workers
    :param loop: asyncio compatible event loop
    :param protocol: subclass of asyncio protocol class
    :param request_class: Request class to use
    :param access_log: disable/enable access log
    :param websocket_max_size: enforces the maximum size for
                               incoming messages in bytes.
    :param websocket_max_queue: sets the maximum length of the queue
                                that holds incoming messages.
    :param websocket_read_limit: sets the high-water limit of the buffer for
                                 incoming bytes, the low-water limit is half
                                 the high-water limit.
    :param websocket_write_limit: sets the high-water limit of the buffer for
                                  outgoing bytes, the low-water limit is a
                                  quarter of the high-water limit.
    :param is_request_stream: disable/enable Request.stream
    :return: Nothing
    """
    if not run_async:
        # create new event_loop after fork
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

    if debug:
        loop.set_debug(debug)

    connections = connections if connections is not None else set()
    server = partial(
        protocol,
        loop=loop,
        connections=connections,
        signal=signal,
        request_handler=request_handler,
        error_handler=error_handler,
        request_timeout=request_timeout,
        response_timeout=response_timeout,
        keep_alive_timeout=keep_alive_timeout,
        request_max_size=request_max_size,
        request_class=request_class,
        access_log=access_log,
        keep_alive=keep_alive,
        is_request_stream=is_request_stream,
        router=router,
        websocket_max_size=websocket_max_size,
        websocket_max_queue=websocket_max_queue,
        websocket_read_limit=websocket_read_limit,
        websocket_write_limit=websocket_write_limit,
        state=state,
        debug=debug,
    )

    create_server_kwargs = dict(
        ssl=ssl,
        reuse_port=reuse_port,
        sock=sock,
        backlog=backlog,
    )

    if constants.is_py37():
        create_server_kwargs.update(
            start_serving=False
        )

    server_coroutine = loop.create_server(
        server, **create_server_kwargs
    )

    # Instead of pulling time at the end of every request,
    # pull it once per minute
    loop.call_soon(partial(update_current_time, loop))

    if run_async:
        return server_coroutine

    try:
        http_server = loop.run_until_complete(server_coroutine)
    except BaseException:
        logger.exception("Unable to start server")
        return

    # Ignore SIGINT when run_multiple
    if run_multiple:
        signal_func(SIGINT, SIG_IGN)

    # Register signals for graceful termination
    if register_sys_signals:
        _signals = (SIGTERM,) if run_multiple else (SIGINT, SIGTERM)
        for _signal in _signals:
            try:
                loop.add_signal_handler(_signal, loop.stop)
            except NotImplementedError:
                logger.warning(
                    "Sanic tried to use loop.add_signal_handler "
                    "but it is not implemented on this platform."
                )

    def start_serving():
        if constants.is_py37():
            loop.run_until_complete(http_server.start_serving())

    def start():
        pid = os.getpid()
        try:
            logger.info("Starting worker [%s]", pid)
            if constants.is_py37():
                loop.run_until_complete(http_server.serve_forever())
            else:
                loop.run_forever()
        finally:
            http_server.close()
            loop.run_until_complete(http_server.wait_closed())
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

    return start_serving, start
示例#3
0
def get_delayed_module_init_class():
    if constants.is_py37():
        return Python37DelayedImport
    else:
        return Python35plusDelayedImport
示例#4
0
 def start_serving():
     if constants.is_py37():
         loop.run_until_complete(http_server.start_serving())
示例#5
0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import pytest

from fdk import constants
from fdk import customer_code

from fdk.tests import funcs


@pytest.mark.skipif(constants.is_py37(),
                    reason="this test is for Python 3.5.2+ only")
def test_py352plus():
    dm = customer_code.Python35plusDelayedImport(funcs.__file__)
    assert dm.executed is False

    m = dm.get_module()
    assert dm.executed is True
    assert m is not None


@pytest.mark.skipif(not constants.is_py37(),
                    reason="this test is for Python 3.7.1+ only")
def test_py37():
    dm = customer_code.Python37DelayedImport(funcs.__file__)
    assert dm.executed is False