Skip to content
Permalink
Browse files

[processing] Only load layers when absolutely required

This change avoids holding onto resources and layers from earlier iterations,
and allows batch processing of many more items then is possible
if we hold on to these layers
  • Loading branch information
nyalldawson committed Nov 10, 2017
1 parent 30cefca commit 3fc90301a995c0c1304ccbf9206d9549fd3daf2d
Showing with 11 additions and 6 deletions.
  1. +11 −6 python/plugins/processing/gui/BatchAlgorithmDialog.py
@@ -80,7 +80,6 @@ def accept(self):
load = []

feedback = self.createFeedback()
context = dataobjects.createContext(feedback)

for row in range(self.mainWidget.tblParameters.rowCount()):
col = 0
@@ -90,7 +89,7 @@ def accept(self):
continue
wrapper = self.mainWidget.wrappers[row][col]
parameters[param.name()] = wrapper.value()
if not param.checkValueIsAcceptable(wrapper.value(), context):
if not param.checkValueIsAcceptable(wrapper.value()):
self.bar.pushMessage("", self.tr('Wrong or missing parameter value: {0} (row {1})').format(
param.description(), row + 1),
level=QgsMessageBar.WARNING, duration=5)
@@ -104,11 +103,11 @@ def accept(self):
count_visible_outputs += 1
widget = self.mainWidget.tblParameters.cellWidget(row, col)
text = widget.getValue()
if out.checkValueIsAcceptable(text, context):
if out.checkValueIsAcceptable(text):
if isinstance(out, (QgsProcessingParameterRasterDestination,
QgsProcessingParameterFeatureSink)):
# load rasters and sinks on completion
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, context.project())
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, QgsProject.instance())
else:
parameters[out.name()] = text
col += 1
@@ -145,6 +144,12 @@ def accept(self):
feedback.pushCommandInfo(pformat(parameters))
feedback.pushInfo('')

# important - we create a new context for each iteration
# this avoids holding onto resources and layers from earlier iterations,
# and allows batch processing of many more items then is possible
# if we hold on to these layers
context = dataobjects.createContext(feedback)

alg_start_time = time.time()
ret, results = execute(self.alg, parameters, context, feedback)
if ret:
@@ -159,9 +164,9 @@ def accept(self):
else:
break

feedback.pushInfo(self.tr('Batch execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
handleAlgorithmResults(self.alg, context, feedback, False)

handleAlgorithmResults(self.alg, context, feedback, False)
feedback.pushInfo(self.tr('Batch execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))

self.finish(algorithm_results)
self.buttonCancel.setEnabled(False)

0 comments on commit 3fc9030

Please sign in to comment.
You can’t perform that action at this time.