Cython,支持我这个错误...我已经展示了我的代码中生成的示例代码和错误。我使用Anaconda3最新发布的Windows,如何在类中使用带有args的线程
#cytest.pyx
from threading import Thread
cdef class t(object):
cdef int a,b,nthreads
cpdef readonly list retlist
def __init__(self,int nthreads,int a=10,int b=10):
self.a = a
self.b = b
self.retlist = []
self.nthreads = nthreads
cdef mul(self,int c,int d):
cdef int n
n = (self.a*c)+(self.b*d)
self.retlist.append(n)
cpdef run_mul(self):
cdef int i
cdef object Thd
for i in range(self.nthreads):
Thd = Thread(target=self.mul, args=(10,20))
# Note: I got error in above arguments
Thd.start()
上面的文件cytest.pyx编译和导入如下..
import cytest
ts = cytest.t(4)
运行以下代码时出现错误,如下所示
ts.run_mul()
如下错误....如何在cython中解决此问题
Exception in thread Thread-11:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in
_bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "stringsource", line 65, in
cfunc.to_py.__Pyx_CFunc_object____t____int____int___to_py.wrap
TypeError: wrap() takes exactly 3 positional arguments (2 given)
Exception in thread Thread-13:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in
_bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "stringsource", line 65, in
cfunc.to_py.__Pyx_CFunc_object____t____int____int___to_py.wrap
TypeError: wrap() takes exactly 3 positional arguments (2 given)
Exception in thread Thread-12:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in
_bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "stringsource", line 65, in
cfunc.to_py.__Pyx_CFunc_object____t____int____int___to_py.wrap
TypeError: wrap() takes exactly 3 positional arguments (2 given)
Exception in thread Thread-10:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in
_bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "stringsource", line 65, in
cfunc.to_py.__Pyx_CFunc_object____t____int____int___to_py.wrap
TypeError: wrap() takes exactly 3 positional arguments (2 given)
答案 0 :(得分:1)
使功能def
代替cdef
。
有助于思考为什么要创建函数cdef
。 cdef
函数唯一真正的区别在于你说它只能在C级调用,所以你可以避免使用Python调用机制。在这种情况下,你试图将它作为可调用的Python对象传递给Python函数,这完全没有意义。
(Cython尝试用Python层包装它来帮助你一点,但这有点儿错误,并没有正确处理绑定self
。你可能想把它报告为bug https://github.com/cython/cython/issues。)