Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gunicorn + argparse not work #2125

Closed
antct opened this issue Oct 7, 2019 · 5 comments
Closed

gunicorn + argparse not work #2125

antct opened this issue Oct 7, 2019 · 5 comments

Comments

@antct
Copy link

antct commented Oct 7, 2019

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
Copy link
Collaborator

tilgovi commented Oct 10, 2019

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
Copy link
Author

antct commented Oct 11, 2019

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
Copy link
Collaborator

tilgovi commented Oct 11, 2019

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

@antct
Copy link
Author

antct commented Oct 12, 2019

Thanks again!

@liuqijie6
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants