-
-
Save YorikSar/51b0b15fad41ef338e7f to your computer and use it in GitHub Desktop.
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
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