From 750bbd64b20de2043bf00bee77e46c93b54e326d Mon Sep 17 00:00:00 2001 From: "roberto@sirius" Date: Fri, 1 Jul 2011 09:05:55 +0200 Subject: [PATCH] new option --import, new api function uwsgi.signal_registered --- decoratortest.py | 31 +++++++++++++++--- plugins/python/python_plugin.c | 19 +++++++++++ plugins/python/uwsgi_pymodule.c | 17 ++++++++++ plugins/python/uwsgi_python.h | 2 ++ signal.c | 8 +++++ utils.c | 19 +++++++++++ uwsgi.h | 4 +++ uwsgidecorators.py | 57 +++++++++++++++++++++++++++++---- 8 files changed, 147 insertions(+), 10 deletions(-) diff --git a/decoratortest.py b/decoratortest.py index b76f1519..a570cc1c 100644 --- a/decoratortest.py +++ b/decoratortest.py @@ -1,4 +1,3 @@ -import uwsgi from uwsgidecorators import * from uwsgicc import app as application @@ -6,23 +5,47 @@ from uwsgicc import app as application import time +# register rpc function helloworld @rpc("helloworld") def hello_world(): return "Hello World" +# register signal 1 @signal(1) def what_time_is_it(num): print(time.asctime()) -@timer(2, 3) +# a 3 seconds timer +@timer(3) def salut(num): print("Salut !!! 3 seconds elapsed and signal %d raised" % num) -@rbtimer(3, 10) +# a 10 seconds red black timer +@rbtimer(10) def tenseconds(num): print("red black timer !!! 10 seconds elapsed and signal %d raised" % num) -@filemon(4, "/tmp") +# monitor /tmp directory +@filemon("/tmp") def tmpmodified(num): print("/tmp has been modified") + + +# spool a long running task +@spool +def a_long_task(vars): + for i in xrange(1,10): + print(i) + time.sleep(1) + +# continuosly spool a long task +@spool +def an_infinite_task(vars): + for i in xrange(1,4): + print("infinite: %d" % i) + time.sleep(1) + + +a_long_task.spool(foo='bar') +an_infinite_task.spool(foo='bar') diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index e5c693c9..b601ce48 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -21,6 +21,10 @@ struct option uwsgi_python_options[] = { {"pythonpath", required_argument, 0, LONG_ARGS_PYTHONPATH}, {"python-path", required_argument, 0, LONG_ARGS_PYTHONPATH}, {"pymodule-alias", required_argument, 0, LONG_ARGS_PYMODULE_ALIAS}, + {"import", required_argument, 0, LONG_ARGS_PYIMPORT}, + {"pyimport", required_argument, 0, LONG_ARGS_PYIMPORT}, + {"py-import", required_argument, 0, LONG_ARGS_PYIMPORT}, + {"python-import", required_argument, 0, LONG_ARGS_PYIMPORT}, {"pp", required_argument, 0, LONG_ARGS_PYTHONPATH}, {"pyargv", required_argument, 0, LONG_ARGS_PYARGV}, {"optimize", required_argument, 0, 'O'}, @@ -720,6 +724,9 @@ int uwsgi_python_manage_options(int i, char *optarg) { uwsgi_log("you can specify at most %d --pymodule-alias options\n", MAX_PYMODULE_ALIAS); } return 1; + case LONG_ARGS_PYIMPORT: + uwsgi_string_new_list(&up.import_list, optarg); + return 1; case LONG_ARGS_PYTHONPATH: if (glob(optarg, GLOB_MARK, NULL, &g)) { uwsgi_string_new_list(&up.python_path, optarg); @@ -813,6 +820,18 @@ void uwsgi_python_init_apps() { up.loaders[LOADER_CALLABLE] = uwsgi_callable_loader; up.loaders[LOADER_STRING_CALLABLE] = uwsgi_string_callable_loader; + struct uwsgi_string_list *upli = up.import_list; + while(upli) { + if (strchr(upli->value, '/') || uwsgi_endswith(upli->value, ".py")) { + uwsgi_pyimport_by_filename("uwsgi_imported_file", upli->value); + } + else { + if (PyImport_ImportModule(upli->value) == NULL) { + PyErr_Print(); + } + } + upli = upli->next; + } if (up.wsgi_config != NULL) { init_uwsgi_app(LOADER_UWSGI, up.wsgi_config, uwsgi.wsgi_req, up.main_thread); diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 6d2dda4b..95da2106 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -460,6 +460,22 @@ PyObject *py_uwsgi_attach_daemon(PyObject * self, PyObject * args) { return Py_True; } +PyObject *py_uwsgi_signal_registered(PyObject * self, PyObject * args) { + uint8_t uwsgi_signal; + + if (!PyArg_ParseTuple(args, "B:signal_registered", &uwsgi_signal)) { + return NULL; + } + + if (uwsgi_signal_registered(uwsgi_signal)) { + Py_INCREF(Py_True); + return Py_True; + } + + Py_INCREF(Py_None); + return Py_None; +} + PyObject *py_uwsgi_register_signal(PyObject * self, PyObject * args) { uint8_t uwsgi_signal; @@ -2393,6 +2409,7 @@ static PyMethodDef uwsgi_advanced_methods[] = { {"register_signal", py_uwsgi_register_signal, METH_VARARGS, ""}, {"signal", py_uwsgi_signal, METH_VARARGS, ""}, {"signal_wait", py_uwsgi_signal_wait, METH_VARARGS, ""}, + {"signal_registered", py_uwsgi_signal_registered, METH_VARARGS, ""}, {"signal_received", py_uwsgi_signal_received, METH_VARARGS, ""}, {"add_file_monitor", py_uwsgi_add_file_monitor, METH_VARARGS, ""}, {"add_timer", py_uwsgi_add_timer, METH_VARARGS, ""}, diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index 0ce04228..a9464b5c 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -13,6 +13,7 @@ #define LONG_ARGS_PYARGV LONG_ARGS_PYTHON_BASE + 3 #define LONG_ARGS_PYMODULE_ALIAS LONG_ARGS_PYTHON_BASE + 4 #define LONG_ARGS_RELOAD_OS_ENV LONG_ARGS_PYTHON_BASE + 5 +#define LONG_ARGS_PYIMPORT LONG_ARGS_PYTHON_BASE + 6 #if PY_MINOR_VERSION == 4 && PY_MAJOR_VERSION == 2 #define Py_ssize_t ssize_t @@ -99,6 +100,7 @@ struct uwsgi_python { char *test_module; struct uwsgi_string_list *python_path; + struct uwsgi_string_list *import_list; PyObject *loader_dict; PyObject* (*loaders[LOADER_MAX]) (void *); diff --git a/signal.c b/signal.c index d6a3fca6..5697f45b 100644 --- a/signal.c +++ b/signal.c @@ -15,6 +15,14 @@ int uwsgi_signal_handler(uint8_t sig) { return uwsgi.p[use->modifier1]->signal_handler(sig, use->handler); } +int uwsgi_signal_registered(uint8_t sig) { + + if (uwsgi.shared->signal_table[sig].handler != NULL) + return 1; + + return 0; +} + int uwsgi_register_signal(uint8_t sig, char *receiver, void *handler, uint8_t modifier1) { struct uwsgi_signal_entry *use = NULL; diff --git a/utils.c b/utils.c index d579f5fb..2bc20497 100644 --- a/utils.c +++ b/utils.c @@ -2434,3 +2434,22 @@ int *uwsgi_attach_fd(int fd, int count, char *code, size_t code_len) { return ret; } + +int uwsgi_endswith(char *str1, char *str2) { + + size_t i; + size_t str1len = strlen(str1); + size_t str2len = strlen(str2); + char *ptr; + + if (str2len > str1len) return 0; + + ptr = (str1 + str1len) - str2len; + + for(i=0;i