在本地运行flask与在AWS Elastic BeanStalk上运行flask-一些范围问题?

时间:2019-05-13 20:17:27

标签: python amazon-web-services scope amazon-elastic-beanstalk

以下代码在我的本地计算机上运行良好:

import tensorflow as tf
import tensorflow_hub as hub
import flask
from flask import request, jsonify

def get_embed_func():
    module_url = "https://tfhub.dev/google/universal-sentence-encoder/2"
    with tf.Graph().as_default():
        sentences = tf.placeholder(tf.string)
        embed = hub.Module(module_url)
        embeddings = embed(sentences)
        session = tf.train.MonitoredSession()
    return lambda x: session.run(embeddings, {sentences: x})


application = flask.Flask(__name__)

@application.route('/', methods=['GET'])
def home():
    return '''<h1>Some header</h1>
<p>A prototype API</p>'''


@application.route('/api/v1', methods=['GET'])
def encoding():
    return jsonify(str(embed_func(['Sample sentence'])[0][0]))


if __name__ == '__main__':
    embed_func = get_embed_func()  
    application.run()

但是在AWS Elastic BeanStalk上,它会生成错误:

[Mon May 13 20:10:55.391791 2019] [:error] [pid 3387] WARNING: Logging before flag parsing goes to stderr.
[Mon May 13 20:10:55.391931 2019] [:error] [pid 3387] W0513 20:10:55.391691 140563746686720 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
[Mon May 13 20:11:41.111770 2019] [:error] [pid 3387] E0513 20:11:41.108649 140563780257536 app.py:1761] Exception on /api/v1 [GET]
[Mon May 13 20:11:41.111801 2019] [:error] [pid 3387] Traceback (most recent call last):
[Mon May 13 20:11:41.111804 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
[Mon May 13 20:11:41.111807 2019] [:error] [pid 3387]     response = self.full_dispatch_request()
[Mon May 13 20:11:41.111810 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
[Mon May 13 20:11:41.111836 2019] [:error] [pid 3387]     rv = self.handle_user_exception(e)
[Mon May 13 20:11:41.111839 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
[Mon May 13 20:11:41.111842 2019] [:error] [pid 3387]     reraise(exc_type, exc_value, tb)
[Mon May 13 20:11:41.111844 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/_compat.py", line 35, in reraise
[Mon May 13 20:11:41.111847 2019] [:error] [pid 3387]     raise value
[Mon May 13 20:11:41.111859 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
[Mon May 13 20:11:41.111862 2019] [:error] [pid 3387]     rv = self.dispatch_request()
[Mon May 13 20:11:41.111864 2019] [:error] [pid 3387]   File "/opt/python/run/venv/local/lib64/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
[Mon May 13 20:11:41.111866 2019] [:error] [pid 3387]     return self.view_functions[rule.endpoint](**req.view_args)
[Mon May 13 20:11:41.111869 2019] [:error] [pid 3387]   File "/opt/python/current/app/application.py", line 28, in endoding
[Mon May 13 20:11:41.111871 2019] [:error] [pid 3387]     return jsonify(str(embed_func(['Sample sentence'])[0][0]))
[Mon May 13 20:11:41.111875 2019] [:error] [pid 3387] NameError: name 'embed_func' is not defined
[Mon May 13 20:11:41.111883 2019] [:error] [pid 3387] 

基本上,以下行在烧瓶范围embed_func = get_embed_func()中未执行或不可见。为什么本地执行与AWS有区别?有解决方案吗?

1 个答案:

答案 0 :(得分:1)

未调用main方法,这就是为什么会出现此错误的原因。是def encoding()使用embed_func变量而不是直接引用get_embed_func()方法的原因吗?

如果将其更改为以下内容,则可能会遇到以下错误:

@application.route('/api/v1', methods=['GET'])
def encoding():
    return jsonify(str(get_embed_func(['Sample sentence'])[0][0]))