diff --git a/decoratortest.py b/decoratortest.py index 45302d66..b18c339b 100644 --- a/decoratortest.py +++ b/decoratortest.py @@ -80,6 +80,15 @@ def fork_happened(): def fork_happened2(): print("fork() has been called [2] wid: %d" % uwsgi.worker_id()) +@postfork +@lock +def locked_func(): + print("starting locked function on worker %d" % uwsgi.worker_id()) + for i in xrange(1, 100): + time.sleep(0.2) + print("[locked %d] waiting..." % uwsgi.worker_id()) + print("done with locked function on worker %d" % uwsgi.worker_id()) + a_long_task.spool({'foo':'bar'}, hello='world') an_infinite_task.spool(foo='bar') a_running_thread() diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 95da2106..b3f2613f 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -895,14 +895,13 @@ PyObject *py_uwsgi_log(PyObject * self, PyObject * args) { PyObject *py_uwsgi_lock(PyObject * self, PyObject * args) { - // the spooler, the master process or single process environment cannot lock resources + // the spooler cannot lock resources #ifdef UWSGI_SPOOLER - if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid && uwsgi.mypid != uwsgi.shared->spooler_pid) { -#else - if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid) { -#endif - uwsgi_lock(uwsgi.user_lock); + if (uwsgi.mypid == uwsgi.shared->spooler_pid) { + return PyErr_Format(PyExc_ValueError, "The spooler cannot lock/unlock resources"); } +#endif + uwsgi_lock(uwsgi.user_lock); Py_INCREF(Py_None); return Py_None; @@ -910,6 +909,12 @@ PyObject *py_uwsgi_lock(PyObject * self, PyObject * args) { PyObject *py_uwsgi_unlock(PyObject * self, PyObject * args) { +#ifdef UWSGI_SPOOLER + if (uwsgi.mypid == uwsgi.shared->spooler_pid) { + return PyErr_Format(PyExc_ValueError, "The spooler cannot lock/unlock resources"); + } +#endif + uwsgi_unlock(uwsgi.user_lock); Py_INCREF(Py_None); diff --git a/uwsgidecorators.py b/uwsgidecorators.py index 3a945470..1d240545 100644 --- a/uwsgidecorators.py +++ b/uwsgidecorators.py @@ -149,6 +149,17 @@ class filemon(object): uwsgi.add_file_monitor(self.num, self.fsobj) return f +class lock(object): + def __init__(self, f): + self.f = f + + def __call__(self, *args, **kwargs): + uwsgi.lock() + try: + return self.f(*args, **kwargs) + finally: + uwsgi.unlock() + class thread(object): def __init__(self, f):