How does tensorflow ignore undefined flags

感情迁移 提交于 2019-12-01 15:24:59

问题


I'm wrapping my tensorflow model in a simple flask server and I'm adding gunicorn wsgi for the flask server. When I ran the gunicorn and tried to send a request to call my train function that has been import to the flask server, I got an error from command line arguments parsing:

absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'b'

I know this flags is passed when gunicorn bind the address arguments, because I have no flags named as 'b' for tensorflow. So my question is how does tensorflow ignore these undefined flags that the tf.app.run() function will not complain?

FYI, Here is my server structure:

wsgi.py:

from simple_server import app

if __name__ == "__main__":
    app.run()

simple_server.py:

from my_tf_model import my_train

@app.route('/call_train', methods=['POST'])
def call_train():
    if request.method == 'POST':
        training_data = request.json
        my_train(training_data, param2)  
        return('Trained!')

my_tf_model.py:

tf.app.flags.DEFINE_integer('model_version',1, 'version number of the model.')
tf.app.flags.DEFINE_string('work_dir', '', 'Working directory.')
FLAGS = tf.app.flags.FLAGS

def my_train(param1, param2):
    # Train Algorithm
    export_path_base = FlAGS.work_dir
    # Exporting model code

def main(argv):

    my_train(param1, param2)

if __name__ == "__main__":
    tf.app.run()

Update:

I'm using tensorflow 1.5.x and python 3.6.0, the command that I used for gunicorn is:

gunicorn -b 0.0.0.0:5000 -t 30 wsgi:app


回答1:


I solved my problem by defining these flags in tensorflow model: my_tf_model.py.

tf.app.flags.DEFINE_string('bind', '', 'Server address')
tf.app.flags.DEFINE_integer('timeout', 30, 'Server timeout')

And then changed my gunicorn command line to use double dash style command line:

gunicorn --bind 0.0.0.0:5000 --timeout 30 wsgi:app

But I think there should be some other way rather than this hack to resolve the globally-used flags.




回答2:


I solved this problem by using gunicorn default config file: gunicorn.conf.py

You can create a config file named gunicorn.conf.py:

bind = 0.0.0.0:5000
timeout = 30

FYI: Settings - Gunicorn documentation

gunicorn_conf.py is the default config file name defined in function gunicorn.config.get_default_config_file, so now you can start your service by command gunicorn wsgi:app.

Now tensorflow knows nothing about gunicorn config.

Notice: this default config name is not mentioned in gunicorn documentation, it's not sure whether this config file name remains unchanged in future version.



来源:https://stackoverflow.com/questions/48592296/how-does-tensorflow-ignore-undefined-flags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!