0

I want to call time-consuming scipy functinons from a dedicated PyQt QThread. AFAIK, when calling scipy functions, they release GIL and Qt event look should work.

Cannot you tell me, how can I cope with it?

# -*- coding: utf-8 -*-

import numpy as np
import scipy as sp
import scipy.interpolate

import os
import sys
import time
from PyQt4 import QtCore, QtGui


class LongRunningWorker(QtCore.QObject):

    result_ready = QtCore.pyqtSignal(tuple)

    @QtCore.pyqtSlot()
    def generate_uber_mega_data(self):
        print "Oy!"
        # time.sleep(5)

        r_ticks = np.linspace(0, 1000, 5000)
        phi_ticks = np.linspace(0, 2 * np.pi, 5000)

        r_grid, phi_grid = np.meshgrid(r_ticks, phi_ticks)

        print u"Starting interpolation"
        spline = sp.interpolate.RectBivariateSpline(r_ticks, phi_ticks, r_grid * phi_grid)
        print u"Интерполяция всё / Interpolation complete"
        time.sleep(5)
        print "Vey!"
        self.result_ready.emit((7, 40))

@QtCore.pyqtSlot(tuple)
def process_mega_data(mega_data):
    print mega_data

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mega_button = QtGui.QPushButton("Press__________________________________________here")
    mega_button.show()

    background_thread = QtCore.QThread()
    worker = LongRunningWorker()
    worker.moveToThread(background_thread)

    background_thread.start()

    # Press here -- and interpolation will start and GUI events on SpinBox will freeze
    mega_button.clicked.connect(worker.generate_uber_mega_data)
    worker.result_ready.connect(process_mega_data)

    # This little scum doesn't change values, when interpolation is running!
    mega_spinbox = QtGui.QSpinBox()
    mega_spinbox.show()

    sys.exit(app.exec_())
Felix
  • 3,351
  • 6
  • 40
  • 68
  • On the first run of the thread, I can reproduce the behaviour you describe (Qt events are blocked by the interpolation running). However, on **subsequent** runs (or at least some subsequent runs) of the thread, it doesn't seem to lock up the GUI. If I switch to the window with the spinbox in, I can change its value while the interpolation runs. Do you see that too? – three_pineapples Mar 29 '15 at 00:24
  • Ok, despite my above comment, I can't see any evidence that the GIL is ever released for that scipy function. I've looked [here](https://github.com/scipy/scipy/blob/9095831d3a8092d70656ec7c5da63892ebe5d051/scipy/interpolate/fitpack2.py#L1054) and [here](https://github.com/scipy/scipy/blob/9095831d3a8092d70656ec7c5da63892ebe5d051/scipy/interpolate/src/fitpack.pyf#L578) but cannot see code that releases the GIL ([this](http://stackoverflow.com/a/15984116/1994235) SO answer details what needs to be done to release the GIL) – three_pineapples Mar 29 '15 at 03:07

0 Answers0