Cython3 Using Threading.Thread with args inside the class

强颜欢笑 提交于 2019-12-08 12:48:16

问题


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)

回答1:


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.)



来源:https://stackoverflow.com/questions/48499164/cython3-using-threading-thread-with-args-inside-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!