Skip to content

Commit

Permalink
[processing] Only load layers when absolutely required
Browse files Browse the repository at this point in the history
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 3fc9030
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions python/plugins/processing/gui/BatchAlgorithmDialog.py
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 3fc9030

Please sign in to comment.