From 0ae7fd760de5f3f4f4cbaaadb0179f2c90da2e47 Mon Sep 17 00:00:00 2001 From: Philip Cristiano Date: Thu, 20 Jun 2013 17:39:46 -0400 Subject: [PATCH] Add post-init hook for workers Allows a config specifying a function after a worker has been initialized and before the run loop --- gunicorn/config.py | 18 ++++++++++++++++++ gunicorn/workers/base.py | 2 ++ 2 files changed, 20 insertions(+) diff --git a/gunicorn/config.py b/gunicorn/config.py index 1ce7e46b..73664038 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -1162,6 +1162,24 @@ class Postfork(Setting): """ +class Postinit(Setting): + name = "post_init" + section = "Server Hooks" + validator = validate_callable(1) + type = six.callable + + def post_init(worker): + pass + + default = staticmethod(post_init) + desc = """\ + Called just after a worker has initialized the application. + + The callable needs to accept one instance variable for the initialized + Worker. + """ + + class PreExec(Setting): name = "pre_exec" section = "Server Hooks" diff --git a/gunicorn/workers/base.py b/gunicorn/workers/base.py index 44d93f8a..da719226 100644 --- a/gunicorn/workers/base.py +++ b/gunicorn/workers/base.py @@ -99,6 +99,8 @@ class Worker(object): self.wsgi = self.app.wsgi() + self.cfg.post_init(self) + # Enter main run loop self.booted = True self.run()