diff --git a/decoratortest.py b/decoratortest.py index 3a0ad1a0..45302d66 100644 --- a/decoratortest.py +++ b/decoratortest.py @@ -53,6 +53,24 @@ def an_infinite_task(args): def one_hour_passed(num): print("received signal %d after 1 hour" % num) +@thread +def a_running_thread(): + while True: + time.sleep(2) + print("i am a no-args thread") + +@thread +def a_running_thread_with_args(who): + while True: + time.sleep(2) + print("Hello %s (from arged-thread)" % who) + +@postfork +@thread +def a_post_fork_thread(): + while True: + time.sleep(3) + print("Hello from a thread in worker %d" % uwsgi.worker_id()) @postfork def fork_happened(): @@ -64,3 +82,5 @@ def fork_happened2(): a_long_task.spool({'foo':'bar'}, hello='world') an_infinite_task.spool(foo='bar') +a_running_thread() +a_running_thread_with_args("uWSGI") diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index b601ce48..f8765f06 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -500,6 +500,11 @@ void init_uwsgi_embedded_module() { exit(1); } + if (PyDict_SetItemString(up.embedded_dict, "has_threads", PyInt_FromLong(uwsgi.has_threads))) { + PyErr_Print(); + exit(1); + } + if (PyDict_SetItemString(up.embedded_dict, "cores", PyInt_FromLong(uwsgi.cores))) { PyErr_Print(); exit(1); @@ -512,39 +517,6 @@ void init_uwsgi_embedded_module() { } } - if (PyDict_SetItemString(up.embedded_dict, "KIND_NULL", PyInt_FromLong(KIND_NULL))) { - PyErr_Print(); - exit(1); - } - if (PyDict_SetItemString(up.embedded_dict, "KIND_WORKER", PyInt_FromLong(KIND_WORKER))) { - PyErr_Print(); - exit(1); - } - if (PyDict_SetItemString(up.embedded_dict, "KIND_EVENT", PyInt_FromLong(KIND_EVENT))) { - PyErr_Print(); - exit(1); - } - if (PyDict_SetItemString(up.embedded_dict, "KIND_SPOOLER", PyInt_FromLong(KIND_SPOOLER))) { - PyErr_Print(); - exit(1); - } - -/* - if (PyDict_SetItemString(up.embedded_dict, "KIND_ERLANG", PyInt_FromLong(KIND_ERLANG))) { - PyErr_Print(); - exit(1); - } -*/ - - if (PyDict_SetItemString(up.embedded_dict, "KIND_PROXY", PyInt_FromLong(KIND_PROXY))) { - PyErr_Print(); - exit(1); - } - if (PyDict_SetItemString(up.embedded_dict, "KIND_MASTER", PyInt_FromLong(KIND_MASTER))) { - PyErr_Print(); - exit(1); - } - PyObject *py_opt_dict = PyDict_New(); for (i = 0; i < uwsgi.exported_opts_cnt; i++) { if (PyDict_Contains(py_opt_dict, PyString_FromString(uwsgi.exported_opts[i]->key))) { diff --git a/uwsgi.h b/uwsgi.h index cccb41b8..582c6051 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1275,13 +1275,6 @@ struct uwsgi_lb_group { }; -#define KIND_NULL 0 -#define KIND_WORKER 1 -#define KIND_EVENT 2 -#define KIND_SPOOLER 3 -#define KIND_PROXY 5 -#define KIND_MASTER 6 - struct uwsgi_signal_entry { uint8_t modifier1; char receiver[64]; diff --git a/uwsgidecorators.py b/uwsgidecorators.py index 410f9293..3a945470 100644 --- a/uwsgidecorators.py +++ b/uwsgidecorators.py @@ -1,4 +1,5 @@ import uwsgi +from threading import Thread if uwsgi.masterpid() == 0: raise Exception("you have to enable the uWSGI master process to use this module") @@ -147,3 +148,16 @@ class filemon(object): uwsgi.register_signal(self.num, self.target, f) uwsgi.add_file_monitor(self.num, self.fsobj) return f + +class thread(object): + + def __init__(self, f): + self.f = f + + def __call__(self, *args): + t = Thread(target=self.f, args=args) + t.daemon = True + t.start() + return self.f + +