Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Created April 15, 2015 13:37
Show Gist options
  • Save ajdavis/1b0854b02953c4366a0f to your computer and use it in GitHub Desktop.
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/
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