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_())