[QGIS-Developer] Long running Python script running a model - moving it to background task?

Nyall Dawson nyall.dawson at gmail.com
Fri Dec 15 18:08:08 PST 2017


On 15 December 2017 at 17:45, Andreas Neumann <a.neumann at carto.net> wrote:

> While this code runs fine, the model also runs for half a minute and I would
> like to use a processing background task, if this is feasible for my code.
> Currently, my code is blocking the UI - a progress bar appears in the map
> canvas (see
> http://www.carto.net/neumann/temp/qgis_running_model_from_python_code.png -
> this was a bit surprising to me - why is this progress bar appearing ? My
> code is not asking for this progress bar?) - and if it appears, shouldn't
> the progress bar be displayed in the status bar instead?

It's just the normal behavior of processing.run - unchanged from 2.x.
You can disable the progress bar by overridding the feedback object to
use one which discards all feedback. E.g.:

f = QgsProcessingFeedback()
processing.run('algid',{parameters},feedback=f)

> Anyway, I guess my code needs to be improved so that it runs as a background
> task utilizing the new QGIS task manager. Are there any "easy to understand"
> Python code examples available that show how to run Python code as a
> background task using QGIS task manager?

There's QgsProcessingAlgRunnerTask, but it takes a few lines of setup
code to utilise. I've been meaning to find time to make an equivalent
of processing.run which runs in the background (only if the algorithm
is safe to do so - see https://github.com/qgis/QGIS/pull/5807). But
that will also be a few lines of code to utilise, since it's non
blocking, you'll need to connect to signals on completion/failure and
continue your script logic from these.

Will try and do this after finalising layout work.

Nyall


More information about the QGIS-Developer mailing list