Cython3在类内部使用带有args的Threading.Thread [英] Cython3 Using Threading.Thread with args inside the class
问题描述
Cython,请为我提供有关此错误的支持...我已经显示了示例代码和在我的代码中生成的错误.我正在使用Windows的Anaconda3最新发行版,以及如何在类内使用带有args的线程
#cytest.pyx从线程导入线程cdef类t(object):cdef int a,b,nthreadscpdef只读列表重新列表def __init __(self,int nthreads,int a = 10,int b = 10):self.a = aself.b = bself.retlist = []self.nthreads = n线程cdef mul(self,int c,int d):cdef int nn =(self.a * c)+(self.b * d)self.retlist.append(n)cpdef run_mul():cdef int我cdef对象Thd对于我在范围内(self.nthreads):Thd =线程(target = self.mul,args =(10,20))#注意:以上参数有误Thd.start()
以上文件cytest.pyx的编译和导入如下.
import cytestts = cytest.t(4)
在运行以下代码时,出现如下错误
ts.run_mul()
错误如下....如何在cython中解决此问题
线程Thread-11中的 Exception:追溯(最近一次通话):文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行916,在_bootstrap_innerself.run()运行中的文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行864self._target(* self._args,** self._kwargs)文件"stringsource",第65行,在cfunc.to_py .__ Pyx_CFunc_object ____ t ____ int ____ int ___ to_py.wrapTypeError:wrap()恰好接受3个位置参数(给定2个)线程Thread-13中的异常:追溯(最近一次通话):文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行916,在_bootstrap_innerself.run()运行中的文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行864self._target(* self._args,** self._kwargs)文件"stringsource",第65行,在cfunc.to_py .__ Pyx_CFunc_object ____ t ____ int ____ int ___ to_py.wrapTypeError:wrap()恰好接受3个位置参数(给定2个)线程Thread-12中的异常:追溯(最近一次通话):文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行916,在_bootstrap_innerself.run()运行中的文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行864self._target(* self._args,** self._kwargs)文件"stringsource",第65行,在cfunc.to_py .__ Pyx_CFunc_object ____ t ____ int ____ int ___ to_py.wrapTypeError:wrap()恰好接受3个位置参数(给定2个)线程Thread-10中的异常:追溯(最近一次通话):文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行916,在_bootstrap_innerself.run()运行中的文件"C:\ ProgramData \ Anaconda3 \ lib \ threading.py",行864self._target(* self._args,** self._kwargs)文件"stringsource",第65行,在cfunc.to_py .__ Pyx_CFunc_object ____ t ____ int ____ int ___ to_py.wrapTypeError:wrap()恰好接受3个位置参数(给定2个)
使函数 def
代替 cdef
.
这有助于考虑为什么要创建函数 cdef
. cdef
函数中唯一真正的区别是您说它只能在C级别上调用,因此可以避免使用Python调用机制.在这种情况下,您尝试将其作为可调用的Python对象传递给Python函数,这是毫无意义的.
(Cython尝试通过用Python层将其包装来为您提供一些帮助,但这有点漏洞,并且无法正确处理绑定的 self
.您可能希望将其报告为 https://github.com/cython/cython/issues 处的错误.)>
Cython,Support me on this error...I have shown the example code and error generated in my code.I'm using Anaconda3 latest distribution for windows, How to use Thread with args inside the class
#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()
Above file cytest.pyx compiled and imported as below..
import cytest
ts = cytest.t(4)
while run the following code I got error as below
ts.run_mul()
Error as below....How to resolve this problem in 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)
Make the function def
instead of cdef
.
It helps to think about why you're making a function cdef
. The only real difference in a cdef
function is that you're saying it can only be called at a C level, and so you can avoid the Python calling mechanism. In this case you're trying to pass it as a callable Python object to a Python function, which is utterly meaningless.
(Cython tries to help you a bit by wrapping it with a Python layer, but this is a bit buggy and doesn't deal with the bound self
correctly. You might want to report it as a bug at https://github.com/cython/cython/issues.)
这篇关于Cython3在类内部使用带有args的Threading.Thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!