benoitc / gunicorn

gunicorn 'Green Unicorn' is a WSGI HTTP Server for UNIX, fast clients and sleepy applications.
http://www.gunicorn.org
Other
9.85k stars 1.75k forks source link

gunicorn + argparse not work #2125

Closed antct closed 5 years ago

antct commented 5 years ago

I try to depoly my pytorch model with argparse module. It seems that the argparse module declared in the app will replace the arguments of gunicorn. And I have encountered such a problem like this:

usage: gunicorn [-h] [--mode MODE] [--batch_size BATCH_SIZE] [--warmup WARMUP]
                [--early_stop EARLY_STOP] [--seed SEED] [--t_total] [--lr LR]
                [--lr_decay_epochs LR_DECAY_EPOCHS]
                [--lr_decay_rate LR_DECAY_RATE]
                [--weight_decay_rate WEIGHT_DECAY_RATE] [--bert_lr BERT_LR]
                [--num_epochs NUM_EPOCHS] [--model_dir MODEL_DIR]
                [--model_path MODEL_PATH] [--num_workers NUM_WORKERS]
                [--train_dataset TRAIN_DATASET] [--eval_dataset EVAL_DATASET]
                [--test_dataset TEST_DATASET] [--bert_low_case BERT_LOW_CASE]
                [--bert_type BERT_TYPE] [--bert_hidden_size BERT_HIDDEN_SIZE]
                [--bert_dropout BERT_DROPOUT]
                [--bert_mention_max_len BERT_MENTION_MAX_LEN]
                [--bert_max_len BERT_MAX_LEN]
                [--bert_char_max_len BERT_CHAR_MAX_LEN]
                [--bert_entity_threshold BERT_ENTITY_THRESHOLD]
                [--bert_freeze] [--bert_threshold BERT_THRESHOLD]
                [--bert_adam] [--enhance_mention] [--interaction]
                [--context_dropout CONTEXT_DROPOUT]
                [--mention_dropout MENTION_DROPOUT]
                [--rnn_hidden_size RNN_HIDDEN_SIZE]
                [--rnn_num_layers RNN_NUM_LAYERS] [--rnn_dropout RNN_DROPOUT]
                [--rnn_num_dirs RNN_NUM_DIRS]
                [--cnn_embedding_dim CNN_EMBEDDING_DIM]
                [--cnn_output_dim CNN_OUTPUT_DIM] [--cnn_filters CNN_FILTERS]
                [--hierarchy_alpha HIERARCHY_ALPHA]
gunicorn: error: unrecognized arguments: -w 1 -b 0.0.0.0:3101 --reload -t 500 api:app

The hints in the above section are the model parameters in the app. Thanks~

tilgovi commented 5 years ago

Can you show how you are creating your application and parsing the CLI with argparse?

Gunicorn calls parse_args on its own instance of argparse. You can implement a custom application that does something else, or you can ensure to mutate sys.argv, or pass your own arguments to parse_args, if you call it after Gunicorn starts.

https://docs.python.org/3/library/argparse.html#the-parse-args-method

antct commented 5 years ago

Can you show how you are creating your application and parsing the CLI with argparse?

Gunicorn calls parse_args on its own instance of argparse. You can implement a custom application that does something else, or you can ensure to mutate sys.argv, or pass your own arguments to parse_args, if you call it after Gunicorn starts.

https://docs.python.org/3/library/argparse.html#the-parse-args-method

Thanks for your reply, I found if I use argparse module in my APP like args = parser.parse_args(sys.argv), the arguments of gunicorn may not work. I try to replace it with args, unknown = parser.parse_known_args(), everything goes well. Thanks ~

tilgovi commented 5 years ago

Can the issue be closed, then? Do you need help to parse your own command line switches and the gunicorn switches together?

antct commented 5 years ago

Thanks again!

liuqijie6 commented 1 year ago

I have a question How do I pass the env parameter through?

gunicorn -c gunicorn.conf.py app:app '/--env=prod/'
2023-03-09 10:04:32.438 | INFO     | config.log_info:info:79 - args: Namespace(env='dev'), unknown: ['-c', 'gunicorn.conf.py', 'app:app', '/--env=prod/']

part of the code

from config.log_info import Logging
args, unknown = parser.parse_known_args()
Logging.info(f"args: {args}, unknown: {unknown}")
return vars(args)