Last active
April 1, 2016 13:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Blocking | |
<hangs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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