Пример #1
0
def start(module_app):
    env = _env.ServingEnv()
    gunicorn_bind_address = HTTP_BIND

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        nginx_config_file = pkg_resources.resource_filename(
            sagemaker_containers.__name__, '/etc/nginx.conf')
        nginx = subprocess.Popen(['nginx', '-c', nginx_config_file])

    gunicorn = subprocess.Popen([
        'gunicorn', '--timeout',
        str(env.model_server_timeout), '-k', 'gevent', '-b',
        gunicorn_bind_address, '--worker-connections',
        str(1000 * env.model_server_workers), '-w',
        str(env.model_server_workers), '--log-level', 'info', module_app
    ])

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
Пример #2
0
def start(module_app):
    env = _env.ServingEnv()
    gunicorn_bind_address = '0.0.0.0:{}'.format(env.http_port)

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        _create_nginx_config(env)
        nginx = subprocess.Popen(['nginx', '-c', nginx_config_file])

    # Install user module before starting GUnicorn
    if env.module_name:
        _modules.import_module(env.module_dir, env.module_name)

    pythonpath = ','.join(sys.path + [_env.code_dir])

    gunicorn = subprocess.Popen([
        'gunicorn', '--timeout',
        str(env.model_server_timeout), '-k', 'gevent', '--pythonpath',
        pythonpath, '-b', gunicorn_bind_address, '--worker-connections',
        str(1000 * env.model_server_workers), '-w',
        str(env.model_server_workers), '--log-level', 'info', module_app
    ])

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
Пример #3
0
def start(module_app):

    env = _env.ServingEnv()
    gunicorn_bind_address = HTTP_BIND

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        nginx_config_file = pkg_resources.resource_filename(sagemaker_containers.__name__, '/etc/nginx.conf')
        nginx = subprocess.Popen(['nginx', '-c', nginx_config_file])

        add_terminate_signal(nginx)

    gunicorn = subprocess.Popen(['gunicorn',
                                 '--timeout', str(env.model_server_timeout),
                                 '-k', 'gevent',
                                 '-b', gunicorn_bind_address,
                                 '--worker-connections', str(1000 * env.model_server_workers),
                                 '-w', str(env.model_server_workers),
                                 '--log-level', 'info',
                                 module_app])

    add_terminate_signal(gunicorn)

    while True:
        if nginx and nginx.poll():
            nginx.terminate()
            break
        elif gunicorn.poll():
            gunicorn.terminate()
            break

    sys.exit(0)
def test_create_nginx_config(tmpdir):
    nginx_config_file = os.path.join(str(tmpdir), 'nginx.conf')
    serving_env = _env.ServingEnv()

    with patch('sagemaker_containers._server.nginx_config_file', nginx_config_file):
        _server._create_nginx_config(serving_env)
        assert os.path.exists(nginx_config_file)
        with open(nginx_config_file, 'r') as f:
            data = f.readline()
            assert data == 'nginx_timeout=4567, nginx_port=1234'
Пример #5
0
def create_serving_env():
    with patch('sagemaker_containers._env.num_cpus',
               lambda: 8), patch('sagemaker_containers._env.num_gpus',
                                 lambda: 4):
        old_environ = os.environ.copy()
        os.environ[_params.USE_NGINX_ENV] = 'false'
        os.environ[_params.MODEL_SERVER_TIMEOUT_ENV] = '20'
        os.environ[_params.CURRENT_HOST_ENV] = 'algo-1'
        os.environ[_params.USER_PROGRAM_ENV] = 'main.py'
        os.environ[_params.SUBMIT_DIR_ENV] = 'my_dir'
        os.environ[_params.ENABLE_METRICS_ENV] = 'true'
        os.environ[_params.REGION_NAME_ENV] = 'us-west-2'

        yield _env.ServingEnv()

        os.environ = old_environ
Пример #6
0
def create_serving_env():
    with patch("sagemaker_containers._env.num_cpus",
               lambda: 8), patch("sagemaker_containers._env.num_gpus",
                                 lambda: 4):
        old_environ = os.environ.copy()
        os.environ[_params.USE_NGINX_ENV] = "false"
        os.environ[_params.MODEL_SERVER_TIMEOUT_ENV] = "20"
        os.environ[_params.CURRENT_HOST_ENV] = "algo-1"
        os.environ[_params.USER_PROGRAM_ENV] = "main.py"
        os.environ[_params.SUBMIT_DIR_ENV] = "my_dir"
        os.environ[_params.ENABLE_METRICS_ENV] = "true"
        os.environ[_params.REGION_NAME_ENV] = "us-west-2"

        yield _env.ServingEnv()

        os.environ = old_environ
Пример #7
0
def start(module_app):
    """Placeholder docstring"""
    env = _env.ServingEnv()
    gunicorn_bind_address = "0.0.0.0:{}".format(env.http_port)

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        _create_nginx_config(env)
        nginx = subprocess.Popen(["nginx", "-c", nginx_config_file])

    # Install user module before starting GUnicorn
    if env.module_name:
        _modules.import_module(env.module_dir, env.module_name)

    pythonpath = ",".join(sys.path + [_env.code_dir])

    gunicorn = subprocess.Popen(
        [
            "gunicorn",
            "--timeout",
            str(env.model_server_timeout),
            "-k",
            "gevent",
            "--pythonpath",
            pythonpath,
            "-b",
            gunicorn_bind_address,
            "--worker-connections",
            str(1000 * env.model_server_workers),
            "-w",
            str(env.model_server_workers),
            "--log-level",
            "info",
            module_app,
        ]
    )

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
Пример #8
0
def start(module_app):
    env = _env.ServingEnv()
    gunicorn_bind_address = '0.0.0.0:{}'.format(env.http_port)

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        _create_nginx_config(env)
        nginx = subprocess.Popen(['nginx', '-c', nginx_config_file])

    if env.preload_app:
        gunicorn = subprocess.Popen([
            'gunicorn', '--timeout',
            str(env.model_server_timeout), '--preload', '-k', 'gevent', '-b',
            gunicorn_bind_address, '--worker-connections',
            str(1000 * env.model_server_workers), '-w',
            str(env.model_server_workers), '--log-level', 'info', module_app
        ])
    else:
        gunicorn = subprocess.Popen([
            'gunicorn', '--timeout',
            str(env.model_server_timeout), '-k', 'gevent', '-b',
            gunicorn_bind_address, '--worker-connections',
            str(1000 * env.model_server_workers), '-w',
            str(env.model_server_workers), '--log-level', 'info', module_app
        ])

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
Пример #9
0
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the 'license' file accompanying this file. This file 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.
from __future__ import absolute_import

import flask
from six.moves import http_client

from sagemaker_containers import _content_types, _env, _logging, _mapping

env = _env.ServingEnv()


def default_healthcheck_fn():  # type: () -> Response
    """Ping is default health-check handler. Returns 200 with no content.

    During a new serving container startup, Amazon SageMaker starts sending periodic GET requests to the /ping endpoint
    to ensure that the container is ready for predictions.

    The simplest requirement on the container is to respond with an HTTP 200 status code and an empty body. This
    indicates to Amazon SageMaker that the container is ready to accept inference requests at the /invocations endpoint.

    If the container does not begin to consistently respond with 200s during the first 30 seconds after startup,
    the CreateEndPoint and UpdateEndpoint APIs will fail.

    While the minimum bar is for the container to return a static 200, a container developer can use this functionality