I wrote a simple test program using thread locks. This program does not behave as expected, and the python interpreter does not complain.
test1.py:
from __future__ import with_statement
from threading import Thread, RLock
import time
import test2
lock = RLock()
class Test1(object):
def __init__(self):
print("Start Test1")
self.test2 = test2.Test2()
self.__Thread = Thread(target=self.myThread, name="thread")
self.__Thread.daemon = True
self.__Thread.start()
self.test1Method()
def test1Method(self):
print("start test1Method")
with lock:
print("entered test1Method")
time.sleep(5)
print("end test1Method")
def myThread(self):
self.test2.test2Method()
if __name__ == "__main__":
client = Test1()
raw_input()
test2.py:
from __future__ import with_statement
import time
import test1
lock = test1.lock
class Test2(object):
def __init__(self):
print("Start Test2")
def test2Method(self):
print("start test2Method")
with lock:
print("entered test2Method")
time.sleep(5)
print("end test2Method")
Both dreams are fulfilled simultaneously! Not what I expected when using lock.
When test2Method moves to test1.py, everything works fine. When I create a lock in test2.py and import it into test1.py, everything works fine. When I create a lock in a separate source file and import it in both test1.py and test2.py, everything works fine.
This is probably due to the circulation.
But why doesn't python complain about this?
source
share