简体   繁体   English

QThreadPool不为空时退出吗?

[英]Quit when QThreadPool not empty?

I have a lot of long running tasks that run in the background of my Python app. 我有许多长时间运行的任务在Python应用程序的后台运行。 I put them all in the global QThreadPool . 我将它们全部放在全局QThreadPool When the user quits, all of those background tasks need to stop. 当用户退出时,所有这些后台任务都需要停止。

Right now, I have the following code: 现在,我有以下代码:

app.aboutToQuit.connect(killAllThreads)

def killAllThreads():
    QtCore.QThreadPool.globalInstance().waitForDone()

I've seen suggestions to add a global variable that says whether the application should be quitting, and to have threads terminate themselves, but this sounds like a terribly messy and inelegant solution. 我已经看到建议添加一个全局变量,该变量说明应用程序是否应该退出,并让线程自行终止,但这听起来像是一个非常凌乱且笨拙的解决方案。 Would you really propose adding a check before every line of code in a background task to make sure that the application shouldn't be quitting yet? 您是否真的建议在后台任务的每一行代码之前添加检查,以确保该应用程序不应该退出? That's hundreds of checks that I would have to add. 那是我必须添加的数百张支票。

The suggestion seems to make the assumption that my tasks are simple and/or have complex clean ups involved, but actually, I have just the opposite: the tasks involve hundreds of lines of code, each of which can take several seconds, but no clean up needs to be done at all. 该建议似乎假设我的任务很简单和/或涉及复杂的清理工作,但实际上,我却恰恰相反:这些任务涉及数百行代码,每行代码可能要花费几秒钟,但没有清理完全需要完成。

I've heard simply killing the threads would be a bad idea, as then they wouldn't be guaranteed to clean up properly, but as no clean up is necessary, that's exactly what I want to do. 我听说简单地杀死线程将是一个坏主意,因为那样就不能保证它们会正确清理,但是由于没有必要清理,这正是我想要做的。 Additionally, race conditions could occur, but again, the tasks need to stop right now, so I really don't care if they end up in an invalid state. 此外,可能会出现竞赛条件,但是同样,这些任务需要立即停止,因此我真的不在乎它们是否以无效状态结束。

So I need to know the following: 因此,我需要了解以下内容:

  1. How do I get a list of all the running threads in a QThreadPool ? 如何获得QThreadPool中所有正在运行的线程的QThreadPool
  2. How do I have them abort what they're doing? 我如何让他们中止他们在做什么?

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. 这个问题的简单答案是,您不能中止QThreadPool中的任何线程,因为它们被包装在QRunnable实例中。 There is no external way to terminate a QRunnable; 没有外部方法可以终止QRunnable。 it has to terminate itself from inside its reimplemented run() method. 它必须从其重新实现的run()方法内部终止自身。

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. 但是,听起来好像在run()方法中run()的任务不适合定期检查标志以查看是否应该终止。

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. 不要使用QThreadPool / QRunnable。

Obviously, choosing (2) implies switching to a more low-level solution, like QThread , and managing the pool of threads yourself. 显然,选择(2)意味着切换到一个更底层的解决方案,例如QThread ,并自己管理线程池。

use daemons, they are automatically terminated when the main thread is ended 使用守护程序,它们在主线程结束时自动终止

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM