我正在尝试使用并行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
但是这引起了一个错误,我不能将cython与QObject一起使用。谁能告诉我如何一起使用cython和pyqt。
https://cython.readthedocs.io/en/latest/src/userguide/extension_types.html#subclassing
如果扩展类型从其他类型继承,则第一个基类必须是内置类型或其他扩展类型
就Cython而言,QObject
是一个神秘的Python类,因此您不能在cdef class
中从它继承(它可能也不能作为第二个或后续的基类,因为它是最终用C / C ++编写,并且此类的多重继承在Python中实际上是非常有限的。]
改为使A
成为常规(而不是cdef
)类。
几乎也肯定会发出需要Python定义的符号,因此需要GIL,因此您将无法(有效)在prange
循环中执行此操作。