[GIS] How to run Processing algorithms in QGIS 3 Plugins

pyqgisqgis-3qgis-pluginsqgis-processing

In QGIS 3 I am trying to run Processing algorithms in a worker thread in a QGIS plugin (not a Processing plugin) using processing.run, and experience crashes.

I guess I need to provide a QgsProcessingContext when running algorithms, but have not found a way to get a proper one.

mycontext = QgsProcessingContext()    
inpbuff = processing.run("native:buffer", {'INPUT': self.inpvl, 'DISTANCE': radius, 'SEGMENTS': 10, 'END_CAP_STYLE': 0, 'JOIN_STYLE': 0, 'MITER_LIMIT': 1, 'DISSOLVE': True, 'OUTPUT': 'memory:'}, feedback=None, context=mycontext)

Both with and without the context parameter, I get many identical warnings before QGIS crashes:

Warning: QObject::setParent: Cannot set parent, new parent is in a different thread

What is the recommended way of running Processing algorithms in a worker thread in a Plugin in QGIS 3?

Best Answer

This seems to have been a problem with early versions of QGIS 3.

With version 3.4.2, I do not get these warnings and crashes when running algorithms in a separate thread in a QGIS plugin.