I had a similar problem: QGIS waits with repainting until the commands started in the same thread finished. An adoption of this very good recipe How do I prevent Qgis from being detected as "not responding" when running a heavy plugin? did the job. Maybe the following works for you. I included a sleep() to slow down the process.
import time
from PyQt4 import QtCore
class Worker(QtCore.QObject):
def __init__(self, layer, *args, **kwargs):
QtCore.QObject.__init__(self, *args, **kwargs)
self.layer = layer
self.abort = False
def run(self):
try:
self.feature_count = self.layer.featureCount()
features = self.layer.getFeatures()
for feature in features:
if self.abort is True:
self.killed.emit()
break
self.layer.setSelectedFeatures([feature.id()])
selected_features = self.layer.selectedFeatures()
self.repaint.emit(feature.id())
time.sleep(0.5)
except:
self.finished.emit(False)
else:
self.finished.emit(True)
def kill(self):
self.abort = True
killed = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal(bool)
repaint = QtCore.pyqtSignal(int)
def repaint(id):
global layer
iface.mapCanvas().zoomToSelected(layer)
try:
path = 'E:\Test' + str(id) +'.png'
iface.mapCanvas().saveAsImage(path)
except:
print 'ERROR: Writing to file %s failed' % path
# more instructions ...
iface.mapCanvas().refresh()
# get active layer
layer = iface.activeLayer()
iface.mapCanvas().setSelectionColor(QColor("yellow"));
thread = QtCore.QThread()
worker = Worker(layer)
worker.moveToThread(thread)
thread.started.connect(worker.run)
worker.finished.connect(worker.deleteLater)
thread.finished.connect(thread.deleteLater)
worker.finished.connect(thread.quit)
worker.repaint.connect(repaint)
thread.start()