From eb0ce609f02064ddc199cbd5f8d96600f8f4199c Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Thu, 4 Aug 2011 11:15:24 +0200 Subject: [PATCH] wakeup spooler (if possibile) after each submission --- plugins/python/python_plugin.c | 25 +++++++++++++++++++++++++ plugins/python/uwsgi_python.h | 2 ++ spooler.c | 21 +++++++++++++++++++++ uwsgi.h | 1 + 4 files changed, 49 insertions(+) diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index b4ba585c..8b15c25c 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -29,6 +29,10 @@ struct option uwsgi_python_options[] = { {"pyimport", required_argument, 0, LONG_ARGS_PYIMPORT}, {"py-import", required_argument, 0, LONG_ARGS_PYIMPORT}, {"python-import", required_argument, 0, LONG_ARGS_PYIMPORT}, + {"spooler-import", required_argument, 0, LONG_ARGS_SPOOLER_PYIMPORT}, + {"spooler-pyimport", required_argument, 0, LONG_ARGS_SPOOLER_PYIMPORT}, + {"spooler-py-import", required_argument, 0, LONG_ARGS_SPOOLER_PYIMPORT}, + {"spooler-python-import", required_argument, 0, LONG_ARGS_SPOOLER_PYIMPORT}, {"pp", required_argument, 0, LONG_ARGS_PYTHONPATH}, {"pyargv", required_argument, 0, LONG_ARGS_PYARGV}, {"optimize", required_argument, 0, 'O'}, @@ -712,6 +716,9 @@ int uwsgi_python_manage_options(int i, char *optarg) { case LONG_ARGS_PYIMPORT: uwsgi_string_new_list(&up.import_list, optarg); return 1; + case LONG_ARGS_SPOOLER_PYIMPORT: + uwsgi_string_new_list(&up.spooler_import_list, optarg); + return 1; case LONG_ARGS_POST_PYMODULE_ALIAS: uwsgi_string_new_list(&up.post_pymodule_alias, optarg); return 1; @@ -816,6 +823,23 @@ char *uwsgi_pythonize(char *orig) { } +void uwsgi_python_spooler_init(void) { + + struct uwsgi_string_list *upli = up.spooler_import_list; + while(upli) { + if (strchr(upli->value, '/') || uwsgi_endswith(upli->value, ".py")) { + uwsgi_pyimport_by_filename(uwsgi_pythonize(upli->value), upli->value); + } + else { + if (PyImport_ImportModule(upli->value) == NULL) { + PyErr_Print(); + } + } + upli = upli->next; + } + +} + void uwsgi_python_init_apps() { struct http_status_codes *http_sc; @@ -1263,6 +1287,7 @@ struct uwsgi_plugin python_plugin = { .resume = uwsgi_python_resume, .hijack_worker = uwsgi_python_hijack, + .spooler_init = uwsgi_python_spooler_init, .signal_handler = uwsgi_python_signal_handler, .rpc = uwsgi_python_rpc, diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 404d5bf4..83d489cc 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -19,6 +19,7 @@ #define LONG_ARGS_PUMP LONG_ARGS_PYTHON_BASE + 9 #define LONG_ARGS_WSGI_LITE LONG_ARGS_PYTHON_BASE + 10 #define LONG_ARGS_PYSHELL LONG_ARGS_PYTHON_BASE + 11 +#define LONG_ARGS_SPOOLER_PYIMPORT LONG_ARGS_PYTHON_BASE + 12 #define PYTHON_APP_TYPE_WSGI 0 #define PYTHON_APP_TYPE_WEB3 1 @@ -128,6 +129,7 @@ struct uwsgi_python { struct uwsgi_string_list *python_path; struct uwsgi_string_list *import_list; + struct uwsgi_string_list *spooler_import_list; struct uwsgi_string_list *post_pymodule_alias; PyObject *loader_dict; diff --git a/spooler.c b/spooler.c index 87e55b1b..786337a7 100644 --- a/spooler.c +++ b/spooler.c @@ -9,6 +9,9 @@ static void spooler_scandir(char *); #endif void spooler_manage_task(char *, char *); +// fake function to allow waking the spooler +void spooler_wakeup() {} + pid_t spooler_start() { int i; @@ -30,6 +33,8 @@ pid_t spooler_start() { exit(1); } else if (pid == 0) { + // USR1 will be used to wake up the spooler + signal(SIGUSR1, spooler_wakeup); uwsgi.mywid = -1; uwsgi.mypid = pid; uwsgi_close_all_sockets(); @@ -42,6 +47,17 @@ pid_t spooler_start() { } } uwsgi.signal_socket = uwsgi.shared->spooler_signal_pipe[1]; + for (i = 0; i < 0xFF; i++) { + if (uwsgi.p[i]->spooler_init) { + uwsgi.p[i]->spooler_init(); + } + } + + for (i = 0; i < uwsgi.gp_cnt; i++) { + if (uwsgi.gp[i]->spooler_init) { + uwsgi.gp[i]->spooler_init(); + } + } spooler(); } else if (pid > 0) { @@ -162,6 +178,11 @@ int spool_request(char *filename, int rn, int core_id, char *buffer, int size, c uwsgi_unlock(uwsgi.spooler_lock); +/* wake up the spooler ... (HACKY) */ + if (uwsgi.shared->spooler_pid > 0 ) { + (void) kill(uwsgi.shared->spooler_pid, SIGUSR1); + } + return 1; diff --git a/uwsgi.h b/uwsgi.h index 991d9f17..ad435600 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -601,6 +601,7 @@ struct uwsgi_plugin { void (*resume) (struct wsgi_request *); void (*hijack_worker) (void); + void (*spooler_init) (void); int (*magic) (char *, char *);