Skip to content

Instantly share code, notes, and snippets.

@YorikSar
Created April 7, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YorikSar/51b0b15fad41ef338e7f to your computer and use it in GitHub Desktop.
Save YorikSar/51b0b15fad41ef338e7f to your computer and use it in GitHub Desktop.
import gc
import threading
import time
lock = threading.Lock()
sig = threading.Event()
class C(object):
def __del__(self):
print "In __del__"
with lock:
print "Got lock in __del__!"
def thread_a():
c = C()
print "Created", c
del c
print "Deleted it"
sig.wait()
print "Starting GC"
gc.collect()
def thread_b():
with lock:
print "Got lock in thread_b"
sig.set()
time.sleep(2)
print "Releasing lock in thread_b"
if __name__ == "__main__":
tha = threading.Thread(target=thread_a)
thb = threading.Thread(target=thread_b)
tha.start()
thb.start()
tha.join()
thb.join()
print "Bye!"
# Results:
# Created <__main__.C object at 0x00007fc6ccf95898>
# Deleted it
# Got lock in thread_b
# Starting GC
# In __del__
# Releasing lock in thread_b
# Got lock in __del__!
# Bye!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment