简体   繁体   中英

Python RecursionError on compiled scripts with Cython

I have a python application which runs perfectly on python but has an error when I run the cythonized scripts.

When I compile the scripts with cython everything compiles ok, I'm compiling on a Raspberry with Linux and running on it too.

setup.py

from distutils.core import setup 
from Cython.Build import cythonize
setup(ext_modules=cythonize(["*.py"]),)

Then I run the setup.sh to compile and I delete all the compiled *.py, *.c and *.pyc just to be sure that what is running is the compiled *.so files. I compile everything except my mainGUI.py it is PySide based and it always breaks while compiling.

setup.sh

sudo python3 ./setup.py build_ext --inplace
find . -name \*.py -delete
find . -name \*.c -delete
sudo rm -r ./__pycache__    
sudo rm -r ./build

Then I run my app

sudo python3 mainGUI.py

And everything seems to be ok, the GUI shows up, menus are working, but when I read the terminal I get this output many times:

RecursionError: maximum recursion depth exceeded while calling a Python object

But I don't know what script.py is throwing this. I've tried leaving some scripts.py out of the compilation and letting them run as .py (for example mySQLdatabaseScript.py) and the amount of RecursionError is reduced but not eliminated.

So even when my GUI runs, and seems without error, most of my functions are not working they throw the RecursionError.

I've read that I can increase the recursion limit

sys.setrecursionlimit(1500)

But where should I do this? in the mainGUI.py? Or on each script.py?

One thing that I can see in this process is that cython is not making my application optimized on memory :/

Thanks :)

I have same error. Exactly the same. I note this happens only when I use a pyside "signal connect" feature. For example, using a timer to display a clock.

timer = QtCore.QTimer(self)
timer.timeout.connect(self.showTime)  #will fail in every showTime Call
timer.start(1000)

I've decided to not use a timer. Ok. It works for now. Same error when connect a double click. It's bad. Same error when connecting a radio button.

Update: Solution, I moved to PyQt5, and planning to pay the license. The Cythonized script works fine with signal - slot feature.

I know this question is fairly old, but for others who would meet this issue, I have found a solution to save the license cost $500... :)

from PySide2.QtWidgets import QWidget


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._timer = QTimer()
        # This is the Magic Code!!!
        setattr(self, "MyWidget._on_timeout", self._on_timeout)
        self._timer.timeout.connect(self._on_timeout)

    def _on_timeout(self):
        # Do your timer
        pass

Apply the magic code to each function that your signal is connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM