具有张量流会话的Flask应用程序无法在PythonAnywhere

时间:2018-03-20 16:13:03

标签: tensorflow flask pythonanywhere

我是新手使用烧瓶,所以请原谅。我有一个来自tensorflow的训练模型,我想加载到我的网络应用程序中,所以我需要启动一次性会话来初始化我的图层变量。我希望能够将发布请求发送到Web应用程序,因此我需要在@ app.route(" /")函数中启动另一个会话。但是,如果我在@ app.route(" /")函数中有会话,则Web应用程序需要很长时间才能刷新,并最终超时。如果我在全局范围内拥有相同的两个会话,则不会发生此问题。

为了澄清,我想我已经在我的app.py文件中隔离了这个问题。以下代码有效:

from flask import Flask
import tensorflow as tf

app = Flask(__name__)

tf_config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=2)

# the first session
with tf.Session(config = tf_config) as sess:
    sess.run(tf.constant([1,2,3])

# a second session immediately following the first one in global scope
with tf.Session(config = tf_config) as sess:
    sess.run(tf.constant([4,5,6])

@app.route("/", methods = ["GET"])
def do_something():
    return "hello from tf"

但是移动第二个tf会话的位置会使刷新超时:

from flask import Flask
import tensorflow as tf

app = Flask(__name__)

tf_config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=2)

# the first session in the same location as before
with tf.Session(config = tf_config) as sess:
    sess.run(tf.constant([1,2,3])

@app.route("/", methods = ["GET"])
def do_something():
    # the second session in local scope; this times out the refresh
    with tf.Session(config = tf_config) as sess:
        sess.run(tf.constant([4,5,6])
    return "hello from tf"

不幸的是,后者是我想要的网络应用程序。我使用的是Python3.6,而且我已经完成了

pip3.6 install --user --upgrade tensorflow

我该如何解决这个问题?

0 个答案:

没有答案