假設我按照http://gunicorn.org/deploy.html#runit開啓gunicorn下的Flask應用程序,有沒有辦法讓我包含/解析/訪問額外的命令行參數?在gunicorn中使用額外的命令行參數
例如,我可以在Flask應用程序中包含並解析foo
選項嗎?
gunicorn mypackage:app --foo=bar
感謝,
假設我按照http://gunicorn.org/deploy.html#runit開啓gunicorn下的Flask應用程序,有沒有辦法讓我包含/解析/訪問額外的命令行參數?在gunicorn中使用額外的命令行參數
例如,我可以在Flask應用程序中包含並解析foo
選項嗎?
gunicorn mypackage:app --foo=bar
感謝,
您不能直接通過命令行參數,但你可以選擇應用程序配置很輕鬆了。
$ gunicorn 'mypackage:build_app(foo="bar")'
將按照預期調用函數「build_app」傳遞foo =「bar」kwarg。這個函數應該返回將被使用的WSGI可調用函數。
我通常在main()
之後把它放在__init.py__
之後,然後我可以使用或不使用gunicorn運行(假設你的main()
也支持其他功能)。
# __init__.py
# Normal entry point
def main():
...
# Gunicorn entry point generator
def app(*args, **kwargs):
# Gunicorn CLI args are useless.
# https://stackoverflow.com/questions/8495367/
#
# Start the application in modified environment.
# https://stackoverflow.com/questions/18668947/
#
import sys
sys.argv = ['--gunicorn']
for k in kwargs:
sys.argv.append("--" + k)
sys.argv.append(kwargs[k])
return main()
這樣你就可以簡單地用
gunicorn 'app(foo=bar)' ...
和你main()
可以使用預計sys.argv
參數標準代碼。
謝謝!在搜索如何傳遞多個參數時到了這裏。結果就像將它們作爲逗號分隔值傳遞一樣簡單;例如, `(foo =「bar」,foo1 =「bar1」)` – 2017-03-28 15:58:52