python - 带有 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。
解决方案
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
循环中(有效地)执行此操作。
推荐阅读
- python - 通过 Python (Spyder IDE) 连接到 Oracle
- python - 如何使用python从csv文件中读取数组列表
- javascript - Angular 6,Materialize:初始化javascript
- sql - 单个空格的正则表达式,然后是数字
- matplotlib - ModuleNotFoundError: No module named 'mpl_toolkits.basemap' 如何安装底图
- mapkit - 苹果地图不显示地图覆盖
- c++ - 在基类中存储指向派生类函数的指针
- python - 将列表中的嵌入列表写为文件中的行
- node.js - 在 Azure 中创建 NodeJS Web 应用程序失败
- windows - 我可以将 iPhone 连接到 Windows 10 机器以查看我在 Flutter 中的工作吗?