Skip to content Skip to sidebar Skip to footer

Quit When Qthreadpool Not Empty?

I have a lot of long running tasks that run in the background of my Python app. I put them all in the global QThreadPool. When the user quits, all of those background tasks need to

Solution 1:

The simple answer to this question is that you cannot abort any of the threads in QThreadPool, because they are wrapped in instances of QRunnable. There is no external way to terminate a QRunnable; it has to terminate itself from inside its reimplemented run() method.

However, it sounds like the tasks running inside your run() method don't lend themselves to periodically checking a flag to see if they should terminate.

If that is the case, you only have two options:

  1. Re-write the tasks in such a way that they can periodically check a flag.
  2. Don't use QThreadPool/QRunnable.

Obviously, choosing (2) implies switching to a more low-level solution, like QThread, and managing the pool of threads yourself.

Solution 2:

use daemons, they are automatically terminated when the main thread is ended

from threading import Thread
t = Thread(target=self.ReadThread)
t.setDaemon(True)

Post a Comment for "Quit When Qthreadpool Not Empty?"