QThread 工作线程中未发出 pyqtSignals

2023-11-30

我有一个实现BackgroundTask对象如下所示:

class BackgroundTask(QObject):
    '''
    A utility class that makes running long-running tasks in a separate thread easier

    :type task: callable
    :param task: The task to run
    :param args: positional arguments to pass to task
    :param kawrgs: keyword arguments to pass to task

    .. warning :: There is one **MAJOR** restriction in task: It **cannot** interact with any Qt GUI objects.
                  doing so will cause the GUI to crash. This is a limitation in Qt's threading model, not with
                  this class's design

    '''
    finished = pyqtSignal() #: Signal that is emitted when the task has finished running

    def __init__(self, task, *args, **kwargs):
        super(BackgroundTask, self).__init__()
        self.task   = task    #: The callable that does the actual task work
        self.args   = args    #: positional arguments passed to task when it is called
        self.kwargs = kwargs  #: keyword arguments pass to task when it is called
        self.results= None    #: After :attr:`finished` is emitted, this will contain whatever value :attr:`task` returned

    def runTask(self):
        '''
        Does the actual calling of :attr:`task`, in the form ``task(*args, **kwargs)``, and stores the returned value
        in :attr:`results`

        '''
        flushed_print('Running Task')
        self.results = self.task(*self.args, **self.kwargs)
        flushed_print('Got My Results!')
        flushed_print('Emitting Finished!')
        self.finished.emit()

    def __repr__(self):
        return '<BackgroundTask(task = {}, {}, {})>'.format(self.task, *self.args, **self.kwargs)


    @staticmethod
    def build_and_run_background_task(thread, finished_notifier, task, *args, **kwargs):
        '''
        Factory method that builds a :class:`BackgroundTask` and runs it on a thread in one call

        :type finished_notifier: callable
        :param finished_notifier: Callback that will be called when the task has completed its execution. Signature: ``func()``
        :rtype: :class:`BackgroundTask`
        :return: The created :class:`BackgroundTask` object, which will be running in its thread.

        Once finished_notifier has been called, the :attr:`results` attribute of the returned :class:`BackgroundTask` should contain
        the return value of the input task callable.

        '''
        flushed_print('Setting Up Background Task To Run In Thread')
        bg_task = BackgroundTask(task, *args, **kwargs)
        bg_task.moveToThread(thread)
        bg_task.finished.connect(thread.quit)
        thread.started.connect(bg_task.runTask)
        thread.finished.connect(finished_notifier)
        thread.start()
        flushed_print('Thread Started!')
        return bg_task

正如我的文档字符串所示,这应该允许我传递任意callable及其论点build_and_run_background_task,并且在任务完成后它应该调用callable通过作为finished_notifier并杀死线程。但是,当我使用以下命令运行它时finished_notifier

def done():
    flushed_print('Done!')

我得到以下输出:

Setting Up Background Task To Run In Thread
Thread Started!
Running Task
Got My Results!
Emitting Finished!

就是这样。 Finished_notifier 回调永远不会被执行,并且线程的 quit 方法永远不会被调用,这表明finshed信号输入BackgroundTask实际上并没有被发射。但是,如果我绑定到finshed直接打电话runTask直接(不在线程中),一切都按预期进行。我确定我错过了一些愚蠢的事情,有什么建议吗?


我自己解决了问题,我需要打电话qApp.processEvents()应用程序中的另一个点正在等待此操作完成。我也在命令行上进行了测试,这掩盖了同样的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

QThread 工作线程中未发出 pyqtSignals 的相关文章

  • 字符串编码和解码?

    这是我对错误消息的尝试 我究竟做错了什么 string decode ascii ignore UnicodeEncodeError ascii 编解码器无法对字符 u xa0 进行编码 位置 37 序数不在范围内 128 string e
  • 在 python matplotlib 中用多种颜色填充多边形

    我正在使用 matplotlib 绘制多边形补丁 并希望表示每个多边形的部分以特定颜色填充 即制作一个饼图 但形状为三角形 正方形或六边形 有没有办法改变饼图的形状或表示多边形的多种填充颜色 Thanks 更新 这是我的意思的模拟 您可以创
  • Python 2.7 将比特币私钥转换为 WIF 私钥

    作为一名编码新手 我刚刚完成了教程 教程是这样的 https www youtube com watch v tX XokHf nI https www youtube com watch v tX XokHf nI 我想用 1 个易于阅读
  • PyMC3-自定义 theano Op 进行数值积分

    我使用 PyMC3 进行参数估计 使用必须定义的特定似然函数 我用谷歌搜索了一下 发现我应该使用densitydist实现用户定义的似然函数的方法 但它不起作用 如何在 PyMC3 中合并用户定义的似然函数并找出最大 aposteriori
  • 异步调用的任务限制?

    我有一个同步工作的 NET 4 5 WCF 客户端 我正在更新它以使用新的异步 等待功能来进行多个同时服务器调用以同时获取数据块 在结束之前 我担心同时运行的所有线程将使服务器饱和 更不用说明年升级到该角色时会终止我的 Azure 辅助角色
  • Qt:关闭期间线程仍在运行时 qthread 被销毁

    我有一堂课 class centralDataPool public QObject Q OBJECT public centralDataPool QObject parent 0 centralDataPool commMonitor
  • Console.ReadKey() 与多线程的奇怪行为

    我在使用时遇到一个奇怪的问题Console ReadKey 在多线程程序中 我的问题是 为什么会发生这种情况 这是一个错误 还是因为我滥用了Console 请注意 控制台是supposed为了线程安全 根据文档 http msdn micr
  • 为什么使用signalfd无法捕获SIGSEGV?

    我的系统是ubuntu 12 04 我将示例修改为man 2 signalfd 并添加sigaddset mask SIGSEGV 在示例中 但我无法得到输出SIGSEGV被生成 这是一个错误吗glibc 源代码片段如下 sigemptys
  • 如何避免 Java 中的忙旋转

    我有一个多线程应用程序 其中一个线程向另一个线程发送消息 等待线程轮询消息并做出反应 处理锁 像这样 等待线程代码 while true if helloArrived System out println Got hello if bye
  • 什么时候需要将参数传递给“Thread.new”?

    在线程外部定义的局部变量似乎从内部可见 因此以下两种用法Thread new似乎是一样的 a foo Thread new puts a gt foo Thread new a a puts a gt foo The document ht
  • 如何在Java中执行特定时间段的任务?

    事实上 我会在确定的时间内执行特定的任务 一组指令 例如 我希望我的程序执行任务 5 分钟 如果它得到正确的结果 它会停止 否则它将继续执行正常任务 5 分钟 最后它告诉我 我怎样才能用Java实现这个 你可以像下面这样 import ja
  • python中嵌套字典值的总和

    我有一本这样的字典 data 11L a 2 b 1 a 2 b 3 22L a 3 b 2 a 2 b 5 a 4 b 2 a 1 b 5 a 1 b 0 33L a 1 b 2 a 3 b 5 a 5 b 2 a 1 b 3 a 1 b
  • Java 线程 JavaDoc

    我编写了一个只能在特定线程上调用的方法 是否应该将标准注释或注释添加到方法的 javadoc 中来表示这一点 不知道有任何这样的标准注释 Java 并发实践 http www javaconcurrencyinpractice com 在第
  • 同一文件的多个文件句柄

    So 这个问题 https stackoverflow com questions 23156116 pypy file append mode让我思考 我对同一个文件的多个文件句柄进行了一些测试 发现了一些奇怪的结果 我希望有人能解释一下
  • 在 python 2 或 python 3 中编写 csv 文件的便携式方法

    在我的 Windows 机器上 我通常在 python 2 中这样做来编写 csv 文件 import csv f open out csv wb cr csv writer f delimiter cr writerow a b c f
  • M:N线程模型真的利用了CPU核心吗?

    有几种线程模型可用于在应用程序中调度线程 1 1 内核级线程 用户创建的每个线程都映射到内核中的调度线程 N 1 用户级线程 用户在单个应用程序中创建的所有线程实际上都映射到单个调度的内核线程 M N 混合螺纹 用户在应用程序中创建的 M
  • C# 线程和队列

    这不是关于我可以或应该使用的不同方法来以最佳方式利用队列 而是关于我所看到的对我来说毫无意义的事情 void Runner member variable queue Queue Synchronized new Queue while t
  • Paramiko - 使用私钥连接 - 不是有效的 OPENSSH 私钥/公钥文件

    我正在尝试找到解决方案 但无法理解我做错了什么 在我的 Linux 服务器上 我运行了以下命令 ssh keygen t rsa 这产生了一个id rsa and id rsa pub file 然后我将它们复制到本地并尝试运行以下代码 s
  • “benaphores”值得在现代操作系统上实施吗?

    当我还是一名 BeOS 程序员时 我读过本文 http www haiku os org legacy docs benewsletter Issue1 26 html Engineering1 26作者 Benoit Schillings
  • TypeError:无法使用抽象方法实例化抽象类 <...>

    这是我的代码 from abc import ABC from abc import abstractmethod class Mamifiero ABC docstring for Mamifiero def init self self

随机推荐