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

Andreas Neumann a.neumann at carto.net
Thu Dec 14 23:45:07 PST 2017


Hi, 

I have a Python script that runs a Processing Model that takes about
20-30 seconds to run. 

Here is my code: 

---------------------- 

def exportAnlagenAlsExcel():
  # Prepare processing framework
  from qgis.core import QgsProcessingFeatureSourceDefinition
  from processing.core.Processing import Processing
  Processing.initialize()
  import processing
  #run processing model
  processing.run("model:Anlagen-Export", { 'ewsanlagelayer' :
'service=\'zg_produktion\' user=\'ews\' password=\'xxx\' sslmode=disable
key=\'t_id\' srid=2056 type=Point table=\'ews\'.\'ews_anlage\'
(geometrie) sql=', 'native:orderbyexpression_1:Ausgabedatei' :
'C:/temp/ews_anlagen.xlsx' })
  #open result file in MS Excel
  command = 'start C:\\temp\\ews_anlagen.xlsx'
  os.system(command)
  iface.messageBar().pushMessage(u"Processing ExcelExport fertig",
level=QgsMessageBar.INFO) 

----------------- 

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? 

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? 

Thanks, 

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20171215/8baeab9c/attachment.html>


More information about the QGIS-Developer mailing list