Home>

Last questionSo I asked about tkinter getting unresponsive.
I have a question related to this.

I decided to run the display on the main thread and the CSV read on the sub thread.

I want to read CSV repeatedly (because the content changes).
I want to read the CSV every 3 minutes.
Therefore, we are currently putting CSV read processing and time.sleep (180) in a while loop.
I can now read it every 3 minutes, but when I close the window it becomes unresponsive.
It closes when sleep () finishes (it cannot be closed during sleep).

However, waiting up to 3 minutes after pressing the close button is an unpleasant specification for app users.

If I want the CSV read interval to be 3 minutes, is it possible to do without sleep ()?
Or is there a way to get rid of no response using sleep ()?

  • Answer # 1

    problem:

    If you do thread.join at the end, you will have to wait for sleep until the thread ends.
    If you do not exit Tkiner during that time, you will be in a non-responsive state.


    Simple measures (how to use sleep () to prevent no response)

    If you create a thread with the "daemon = True" option without "thread.join" at the end
    At the end, the thread is forcibly terminated.

    * However, if the writing process is performed on the sub thread side,
    It may cause data corruption.

    If it's read-only, it's relatively safe, but there are other things we're doing,
    If you don't know if it will affect you, you should avoid it.
    If it is terminated while the file is open, it will be forcibly terminated.
    → Causes (possibly) bugs that occur depending on the timing.


    Solution (realized without sleep ())

    Make threads listen for queue reads

    When the app is closed, send a message to exit the queue standby loop

    Every 60 seconds, schedule with Tkinter's timer (after)
    Notify the queue of the read process.