Home > Net >  CPython extension using omp freezes Qt UI
CPython extension using omp freezes Qt UI

Time:03-23

I am working on a scientific algorithm (image processing), which is written in C , and uses lots of parallelization, handled by OpenMP. I need it to be callable from Python, so I created a CPython package, which handles the wrapping of the algorithm. Now I need some UI, as user interaction is essential for initializing some stuff. My problem is that the UI freezes when I run the algorithm. I start the algorithm in a separate thread, so this shouldn't be a problem (I even proved it by replacing the function call with time.sleep, and it works fine, not causing any freeze). For testing I reduced the UI to two buttons: one for starting the algorithm, and another just to print some random string to console (to check UI interactions).
I also experienced something really weird. If I started moving the mouse, then pressed the button to start the computation, and after that kept moving the mouse continuously, the UI did not freeze, so hovering over the buttons gave them the usual blueish Windows-style tint. But if I stopped moving my mouse for a several seconds over the application window, clicked a button, or swapped to another window, the UI froze again. It's even more strange that the UI stayed active if I rested my mouse outside of the application window.
Here's my code (unfortunately I cannot share the algorithm for several reasons, but I hope I manage to get some help even like this):

if __name__ == "__main__":
    from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
    from PyQt5.QtCore import QThread, QObject, pyqtSignal
    import time
    from CustomAlgorithm import Estimator # my custom Python package implemented in C  

    class Worker(QObject):
        finished = pyqtSignal()

        def run(self):
            estimator = Estimator()
            estimator.calculate()
            # if the above two lines are commented, and the next line is uncommented,
            # everything's fine
            # time.sleep(5)
            print("done")

    app = QApplication([])
    thread = QThread()
    window = QWidget()
    layout = QVBoxLayout()
    # button to start the calculation
    btn = QPushButton("start")
    layout.addWidget(btn)
    btn.clicked.connect(thread.start)

    # button to print some text to console
    btn2 = QPushButton("other button")
    layout.addWidget(btn2)
    btn2.clicked.connect(lambda: print("other button clicked"))
    window.setLayout(layout)

    # handling events
    worker = Worker(app)
    worker.moveToThread(thread)
    thread.started.connect(worker.run)
    worker.finished.connect(thread.quit)
    worker.finished.connect(worker.deleteLater)
    thread.finished.connect(thread.deleteLater)
    window.show()
    app.exec_()

I tried multiple variants of using threads, like threading.Thread, multiprocessing.Process, PyQt5.QtCore.QThread (as seen above), even napari's worker implementation, but the result was the same. I even tried removing omp from the code, just in case it interferes somehow with python threads, but it didn't help.

As for the reason I use python, is that the final goal is to make my implementation available in napari.

Any help would be highly appreciated!

CodePudding user response:

Because of Python's "Global Interpreter Lock", only one thread can run Python code at a time. However, other threads can do I/O at the same time.

If you want to allow other threads to run (just like I/O does) you can surround your code with these macros:

Py_BEGIN_ALLOW_THREADS

// computation goes here

Py_END_ALLOW_THREADS

Other Python threads will be allowed to run while the computation is happening. You can't access anything from Python between these two lines - so get that data in order before Py_BEGIN_ALLOW_THREADS.

Reference

  • Related