Skip to content

Instantly share code, notes, and snippets.

@schedutron
Last active August 13, 2017 03:11
Show Gist options
  • Save schedutron/5ac091bc2acbe4f67deb4d14e01419d2 to your computer and use it in GitHub Desktop.
Save schedutron/5ac091bc2acbe4f67deb4d14e01419d2 to your computer and use it in GitHub Desktop.
A demo script for threading.RLock
import threading
num = 0
lock = Threading.Lock()
lock.acquire()
num += 1
lock.acquire() # This will block.
num += 2
lock.release()
# With RLock, that problem doesn’t happen.
lock = Threading.RLock()
lock.acquire()
num += 3
lock.acquire() # This won’t block.
num += 4
lock.release()
lock.release() # You need to call release once for each call to acquire.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment