Search code examples
pythonlockinggevent

gevent-compatible reentrant lock?


What is the RLock equivalent in gevent? If there is no equivalent, how to identify the current greenlet so I can implement one?

From reading the source, gevent.lock.BoundedSemaphore(1) is the equivalent of a simple (non-reentrant) Lock. Testing also indicates this.


Solution

  • The gevent.lock module has an implementation of RLock which uses the getcurrent method of gevent.hub to identify the current greenlet. Here is a simple example.

    from gevent.lock import RLock
    
    lock = RLock()
    with lock:
         print("acquired once")
         with lock:
              print("acquired twice")
    

    While I didn't find any documentation here, you can always read the source.