5

The Python multiprocessing module has a class for reentrant/recursive locks:

from multiprocessing import RLock

l = RLock()
l.acquire()
l.acquire()
l.release()
l.release()

This works great for processes that were forked from a common parent process and, hence, can share the same RLock object. However, for situations with independent processes (example: web server + cron job), one would need a named lock. Unfortunately, RLock() does not accept a name argument for the lock. Is there a solution that allows to do something like this?

l = RLock('mylock')
l.acquire()
l.release()
weatherfrog
  • 2,970
  • 2
  • 19
  • 17
  • did you read https://stackoverflow.com/questions/6931342/system-wide-mutex-in-python-on-linux ? – hiro protagonist Jun 04 '15 at 10:59
  • I did. And while it brought some clarity, it did not answer the question. However, I found [this](http://semanchuk.com/philip/posix_ipc/) library. The `Semaphore` class might be what I'm looking for. – weatherfrog Jun 04 '15 at 16:34

1 Answers1

1

Check out oslo_concurrency.lockutils. It has a lock context manager and a synchronized decorator, both of which take a name and other handy interprocess-friendly parameters.

Eric Fried
  • 11
  • 2