本文介绍了重用 QRunnable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验 QRunnable 以便启动和运行一些服务调用.我从 Qt 文档中偶然发现了以下信息:

I have been experimenting with QRunnable in order to get some service calls up and running. I have stumbled across the following piece of information from the Qt documentation:

QThreadPool 支持多次执行相同的 QRunnable从 QRunnable::run() 中调用 tryStart(this).如果自动删除是启用 QRunnable 将在最后一个线程退出时被删除运行功能.使用相同的 QRunnable 多次调用 start()启用 autoDelete 时会产生竞争条件,而不是推荐.

有人可以解释一下这是什么意思吗?我编写了以下代码,它允许我按顺序多次执行 QRunnable 对象:

Can someone explain what this means? I've written the following code and it allows me to execute a QRunnable object multiple times sequentially:

#!/usr/bin/env python

from PyQt4.QtCore import QRunnable, pyqtSlot, pyqtSignal, QObject, QThread, QThreadPool
from PyQt4.QtGui import QApplication, QWidget, QPushButton, QHBoxLayout, QLabel
from sys import exit, argv
from random import getrandbits

class ServiceCallSignals(QObject):
  srv_status = pyqtSignal(bool)
  srv_running = pyqtSignal(bool)

class ServiceCall(QRunnable):

  def __init__(self):
    super(ServiceCall, self).__init__()
    self.signals = ServiceCallSignals()

  def run(self):
    self.signals.srv_running.emit(True)
    call = bool(getrandbits(1))
    print('QRunnable Thread ID: %d' % int(QThread.currentThreadId()))
    if call: QThread.sleep(5)

    self.signals.srv_status.emit(call)
    self.signals.srv_running.emit(False)

class Test(QWidget):

  def __init__(self):
    super(Test, self).__init__()
    self.initUI()

  def initUI(self):

    layout = QHBoxLayout(self)

    self.cb = QPushButton('Send request', self)
    self.cb.clicked.connect(self.srv_send)
    layout.addWidget(self.cb)

    self.lbl = QLabel('Waiting...', self)
    layout.addWidget(self.lbl)

    self.srv = ServiceCall()
    self.srv.setAutoDelete(False)
    self.srv.signals.srv_status.connect(self.srv_receive)
    self.srv.signals.srv_running.connect(self.srv_block)
    self.tp = QThreadPool(self)

    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('QRunnable and ROS service calls')
    self.show()

  @pyqtSlot()
  def srv_send(self):
    print('Main Thread ID: %d' % int(QThread.currentThreadId()))
    self.tp.start(self.srv)
    self.cb.setText('Running for reply')

  @pyqtSlot(bool)
  def srv_block(self, state):
    self.cb.setEnabled(not state)

  @pyqtSlot(bool)
  def srv_receive(self, srv_res):
    if srv_res: self.lbl.setText('Success')
    else: self.lbl.setText('Failed')
    self.cb.setText('Send request')

def main():

  app = QApplication(argv)
  t = Test()
  exit(app.exec_())


if __name__ == '__main__':
  main()

文档中的引用是否意味着我做错了?如果我把我的 QThreadPool 和使用 tryStart(self)在我的跑步中,我运行了很多很多线程......

Does the quote from the documentation mean that I'm doing it wrong? If I put my QThreadPool and use tryStart(self) inside my run I get many, many threads running...

推荐答案

文档说它支持run()tryStart>,并不是必须.

The documentation is saying that it supports calling tryStart inside run(), not that it is required.

如果您在 run() 之外依次调用 start(),它将重复使用相同的线程.但是如果你在 run() 中调用 tryStart(),它可能会在必要时保留额外的线程.

If you sequentially call start() outside of run() it will re-use the same thread. But if you call tryStart() within run() it may reserve additional threads if necessary.

这篇关于重用 QRunnable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:07