Created
April 15, 2015 13:37
-
-
Save ajdavis/1b0854b02953c4366a0f to your computer and use it in GitHub Desktop.
Demonstrate the interaction between GC and threading in Python. Followup to http://emptysqua.re/blog/pypy-garbage-collection-and-a-deadlock/
This file contains hidden or 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
import threading | |
import gc | |
from time import sleep | |
lock = threading.RLock() | |
def target(): | |
print 'target' | |
with lock: | |
sleep(2) | |
t = threading.Thread(target=target) | |
t.daemon = True | |
t.start() | |
sleep(1) # Wait for 'target' to get lock. | |
class C(object): | |
def __del__(self): | |
print('getting lock') | |
with lock: | |
print('releasing lock') | |
pass | |
c = C() | |
del c | |
with lock: | |
print('collecting') | |
gc.collect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment