简体   繁体   中英

Detect if flask is being run via gunicorn?

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).

晚会晚了,但是似乎很有效的解决方案很有效:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM