Python PySide/PyQt中的qt并发

Python PySide/PyQt中的qt并发,python,pyside,qtconcurrent,Python,Pyside,Qtconcurrent,我正在试图弄清楚,在其中子类化和编写run方法是否有效: class Task(QtCore.QtConcurrent): def run(self, function): function() 还是完全没有用?它完全没有用,因为它是一个名称空间,而不是一个类 此外,PyQt和PySide都不提供QtConcurrent提供的任何功能,因为它们都是基于模板的,因此无法包装 PS:您链接到的PySide文档是针对枚举的。由于该枚举是否在QtConcurrent名

我正在试图弄清楚,在其中子类化和编写run方法是否有效:

class Task(QtCore.QtConcurrent):

     def run(self, function):
           function()

还是完全没有用?

它完全没有用,因为它是一个名称空间,而不是一个类

此外,PyQt和PySide都不提供
QtConcurrent
提供的任何功能,因为它们都是基于模板的,因此无法包装


PS:您链接到的PySide文档是针对枚举的。由于该枚举是否在
QtConcurrent
名称空间之外有任何用途是值得怀疑的,因此PySide包含它可能是一个bug。

您正在查找的类是。

我在PyQt5中遇到了相同的问题。我想唯一的解决办法是在本地进行:

def connect(self):
    class ConnectThread(QThread):
        def __init__(self, func):
            super().__init__()
            self.func = func
        def run(self):
            self.func()
    self.connectThread = ConnectThread(self._connect)
    self.connectThread.start()

def _connect(self):
    if self._driver is None:
        uri = self.uriString()
        if uri and self.user and self.password:
            self.statusMessage.emit("Connecting to the Graph Database....", -1, "color:blue;")
            try:
                self._driver = GraphDatabase.driver(uri, auth=(self.user, self.password))
                self.statusMessage.emit("Connected!", 5000, "color:green;")
            except Exception as e:
                self.clearStatusMessage.emit()
                Error(str(e)).exec_()
                if __debug__:
                    raise e
记住将线程设置为一个成员变量:
self.thread=…
,否则线程引用将超出范围,很可能线程对象被删除


您还可以将函数移动到它的本地定义中,因为Python允许嵌套函数和类在彼此之间

你为什么不试试呢?很有趣。我正要在PyQt5中对
QtConcurrent
进行一点实验,这时我意识到我什么也找不到。谢谢你的提示。我猜它是C++。数据不提供调用任何函数的能力我不认为。。。这里唯一的选项是从QThread继承。请参阅上面我的解决方案。