Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize model once #51

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/sagemaker_chainer_container/serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,18 @@ def _user_module_transformer(user_module):
output_fn=output_fn)


def main(environ, start_response):
serving_env = env.ServingEnv()

logger.setLevel(serving_env.log_level)
app = None

user_module = modules.import_module(serving_env.module_dir, serving_env.module_name)

user_module_transformer = _user_module_transformer(user_module)

user_module_transformer.initialize()
def main(environ, start_response):
global app
if app is None:
serving_env = env.ServingEnv()
logger.setLevel(serving_env.log_level)
user_module = modules.import_module(serving_env.module_dir, serving_env.module_name)
user_module_transformer = _user_module_transformer(user_module)
user_module_transformer.initialize()
app = worker.Worker(transform_fn=user_module_transformer.transform,
module_name=serving_env.module_name)

app = worker.Worker(transform_fn=user_module_transformer.transform,
module_name=serving_env.module_name)
return app(environ, start_response)
21 changes: 21 additions & 0 deletions test/integration/local/test_serving.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import absolute_import

import os

import requests

from test.utils import local_mode

path = os.path.dirname(os.path.realpath(__file__))
resources_path = os.path.abspath(os.path.join(path, '..', '..', 'resources'))


def test_serving_calls_model_fn_once(docker_image, opt_ml):
script_path = os.path.join(resources_path, 'call_model_fn_once.py')
with local_mode.serve(script_path, model_dir=None, image_name=docker_image, opt_ml=opt_ml,
additional_env_vars=['SAGEMAKER_MODEL_SERVER_WORKERS=2']):

# call enough times to ensure multiple requests to a worker
for i in range(3):
# will return 500 error if model_fn called during request handling
assert b'output' == requests.post(local_mode.REQUEST_URL, data=b'input').content
36 changes: 36 additions & 0 deletions test/resources/call_model_fn_once.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# 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.

import os


def model_fn(model_dir):
lock_file = os.path.join(model_dir, 'model_fn.lock.{}'.format(os.getpid()))
if os.path.exists(lock_file):
raise RuntimeError('model_fn called more than once (lock: {})'.format(lock_file))

open(lock_file, 'a').close()

return 'model'


def input_fn(data, content_type):
return data


def predict_fn(data, model):
return b'output'


def output_fn(prediction, accept):
return prediction