Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

named reentrant/recursive lock (RLock) in Python

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()
like image 542
weatherfrog Avatar asked Nov 09 '22 12:11

weatherfrog


1 Answers

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.

like image 72
Eric Fried Avatar answered Nov 14 '22 22:11

Eric Fried