Skip to content

Instantly share code, notes, and snippets.

@elzibubble
Last active April 1, 2016 13:37
Show Gist options
  • Save elzibubble/9d2458834e13ad0f5e25 to your computer and use it in GitHub Desktop.
Save elzibubble/9d2458834e13ad0f5e25 to your computer and use it in GitHub Desktop.
Show that an unpatched RLock blocks the native thread not just the greenlet
from __future__ import print_function
import os
import threading
import time
import eventlet
def block(lock):
print("Blocking")
with lock:
return 'ok'
def zzz(greenlet):
print("Sleeping")
time.sleep(1)
if not greenlet.dead:
greenlet.kill()
def test(key, lock):
with lock:
blocker = eventlet.spawn(block, lock)
sleeper = eventlet.spawn(zzz, blocker)
try:
return blocker.wait()
except eventlet.greenlet.GreenletExit as e:
return 'killed'
def main():
os.system('clear')
lock = threading.RLock()
eventlet.monkey_patch()
# print(test('unpatched RLock', lock))
lock = threading.RLock()
print(test('patched RLock', lock))
if __name__ == '__main__':
main()
Blocking
Sleeping
<waits 1 second>
killed
<exits cleanly>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment