From 06987d3702dcae44e8a87566f1f911b98a9f489e Mon Sep 17 00:00:00 2001 From: Neil Chintomby Date: Mon, 14 Mar 2011 14:37:19 -0400 Subject: [PATCH] add server hook "on_starting" to allow socket patching to allow gevent worker reloading. --- examples/example_gevent_reloader.py | 5 +++++ gunicorn/arbiter.py | 1 + gunicorn/config.py | 16 ++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/examples/example_gevent_reloader.py b/examples/example_gevent_reloader.py index 146f4c72..b303c2f0 100644 --- a/examples/example_gevent_reloader.py +++ b/examples/example_gevent_reloader.py @@ -3,6 +3,11 @@ import os import signal import sys +def on_starting(server): + # use server hook to patch socket to allow worker reloading + from gevent import monkey + monkey.patch_socket() + def when_ready(server): def monitor(): modify_times = {} diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 05f6a2e2..55fd9ffb 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -109,6 +109,7 @@ class Arbiter(object): """\ Initialize the arbiter. Start listening and set pidfile if needed. """ + self.cfg.on_starting(self) self.pid = os.getpid() self.init_signals() if not self.LISTENER: diff --git a/gunicorn/config.py b/gunicorn/config.py index 029556fe..c16d98c6 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -703,3 +703,19 @@ class WorkerExit(Setting): The callable needs to accept two instance variables for the Arbiter and the just-exited Worker. """ + +class OnStarting(Setting): + name = "on_starting" + section = "Server Hooks" + validator = validate_callable(1) + type = "callable" + def def_on_starting(server): + pass + def_on_starting = staticmethod(def_on_starting) + default = def_on_starting + desc = """\ + Called just before the master process is initialized. + + The callable needs to accept a single instance variable for the Arbiter. + """ +