From f9a843dfd55a970e08057347d598a48aed0dddc4 Mon Sep 17 00:00:00 2001 From: "roberto@maverick64" Date: Fri, 1 Jul 2011 10:03:16 +0200 Subject: [PATCH] added @postfork decorator --- decoratortest.py | 10 ++++++++++ uwsgidecorators.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/decoratortest.py b/decoratortest.py index 19db2a79..625e39bf 100644 --- a/decoratortest.py +++ b/decoratortest.py @@ -1,3 +1,5 @@ +import uwsgi + from uwsgidecorators import * from uwsgicc import app as application @@ -52,5 +54,13 @@ def one_hour_passed(num): print("received signal %d after 1 hour" % num) +@postfork +def fork_happened(): + print("fork() has been called [1]") + +@postfork +def fork_happened2(): + print("fork() has been called [2] wid: %d" % uwsgi.worker_id()) + a_long_task.spool({'foo':'bar'}, hello='world') an_infinite_task.spool(foo='bar') diff --git a/uwsgidecorators.py b/uwsgidecorators.py index 95914f44..4f4c1cbb 100644 --- a/uwsgidecorators.py +++ b/uwsgidecorators.py @@ -7,6 +7,7 @@ if uwsgi.opt.get('lazy'): raise Exception("uWSGI lazy mode is not supporte by this module") spooler_functions = {} +postfork_chain = [] def get_free_signal(): for signum in xrange(0, 256): @@ -21,8 +22,16 @@ def manage_spool_request(vars): return ret return spooler_functions[vars['ud_spool_func']].ret -uwsgi.spooler = manage_spool_request +def postfork_chain_hook(): + for f in postfork_chain: + f() +uwsgi.spooler = manage_spool_request +uwsgi.post_fork_hook = postfork_chain_hook + +class postfork(object): + def __init__(self, f): + postfork_chain.append(f) class spool(object):