I'm new to using flask, so be forgiving. I have a trained model from tensorflow that I want to load into my web app, so I need to start a one-time session to initialize my layer variables. I want to be able send post requests to the web app, so I need to start another session in the @app.route("/") function. However, if I have a session in the @app.route("/") function, the web app takes a long time to refresh, and eventually times out. This problem does not happen if I have the same two sessions in global scope.
To clarify, I think I've isolated the problem in my app.py file. The following code works:
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"
But moving the location of the second tf session makes the refresh time out:
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"
Unfortunately, the latter is what I want in my web app. How would I fix this?
Edit: I almost forgot: I'm using Python3.6, and I already did
pip3.6 install --user --upgrade tensorflow