Is there anything wrong with the following code? Running similar code I get a deadlock, where the loop does not relase its lock if the main thread tries to acquire the locked lock.
Also, since I cannot post this without "adding more details", what would some good details be in this case?
import Tkinter
import threading
from time import sleep
from random import random
class Test(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.resizable(False, False)
self.grid()
self._button = Tkinter.Button(self, text='Click me', command=self._foo)
self._button.grid(column=0, row=0)
self._lock = threading.Lock()
def __enter__(self):
self._loopRunning = True
self._loopThread = threading.Thread(target=self._loop)
self._loopThread.start()
return self
def __exit__(self, exc_type, exc_val, traceback):
self._loopRunning = False
self._loopThread.join()
return False
def _loop(self):
while self._loopRunning:
print 'loop wants lock'
with self._lock:
print 'loop acquired lock'
self._button.configure()
sleep(random())
print 'loop should release lock'
print 'loop released lock'
sleep(1)
def _foo(self):
print 'foo wants lock'
with self._lock:
print 'foo acquired lock'
self._button.configure()
sleep(random())
print 'foo should release lock'
print 'foo released lock'
sleep(1)
if __name__ == '__main__':
with Test(None) as test:
test.mainloop()