首页 > 解决方案 > 带有 pyqt 信号的 Cython

问题描述

我正在尝试使用并行 cython 来运行昂贵的功能。我想在课堂上加入一个信号。如下所示:

cdef class A(QObject):

    finish=pyqtSignal(float)

    def counter(self, int count): # define the type of input

        # Define all variables
        cdef int x = 0
        cdef int i
        for i in prange (count, schedule='dynamic', nogil=True):
            if i>=5000000: # capture any error
                x=100
            else:
                x += i
                self.finish.emit(i)
        return x

但它会引发一个错误,说我不能将 QObject 与 cython 一起使用。谁能告诉我如何一起使用 cython 和 pyqt。

标签: pythonpyqtpyqt5cython

解决方案


https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#subclassing

如果扩展类型继承自其他类型,则第一个基类必须是内置类型或其他扩展类型

就 Cython 而言QObject,它是一个神秘的 Python 类,因此您不能在 a 中继承它cdef class(它可能也不能作为第二个或后续基类,因为它最终是用 C/C++ 编写的,并且此类的多重继承类在 Python 中实际上非常有限)。

改为A进行常规(非cdef)课程。


几乎可以肯定,发出 Python 定义的符号也需要 GIL,因此您将无法在prange循环中(有效地)执行此操作。


推荐阅读