Detect if flask is being run via gunicorn?

一笑奈何 提交于 2019-12-10 02:00:50

问题


Is there a way I can check if my flask app is being run instide a gunicorn container? Currently I set an enviroment variable to tell my application this, but I'd prefer that it be automatic. Additionally, is there someway I can check what worker class is being used?


I need to detect this for a few different reasons. Note that typically I use gunicorn, but during testing I won't sometimes.

  1. Logging: I attach to a gunicorn info log when run in gunicorn, otherwise to a stdout log.
  2. Eventlet/subprocess: Since I use subprocesses I need to ensure that the proper monkey_patch'ing is done when using eventlet, otherwise it doesn't behave correctly. (I call many subprocesses).

回答1:


Late to the party, but a very hacky solution that seems to work:

is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")


来源:https://stackoverflow.com/questions/22192519/detect-if-flask-is-being-run-via-gunicorn

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