Question

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()
Was it helpful?

Solution

Tkinter is not thread safe. Use a queue for Tkinter manipulations or use the thread safe version mtTkinter (http://tkinter.unpythonic.net/wiki/mtTkinter) instead. (I went with mtTkinter.)

Apologies for not looking enough at previously asked questions. The problem had been addressed before, e.g. at:

Threaded Tkinter script crashes when creating the second Toplevel widget

Possible locking issue in Tkinter application

Tkinter: How to use threads to preventing main event loop from "freezing"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top