[QGIS-Developer] Questions about using QgsTask class

Nicolae Marinică nicolae.marinica at qtibia.ro
Fri Jul 16 06:35:58 PDT 2021


Dear QGIS developers,

My name is Nicolae Marinica (Nicu) from QTIBIA Engineering and I recently started to work with QGIS. While developing a new QGIS plugin, I stumbled upon some problems when using QgsTask. Although my background is in the embedded world (with C/C++ and Python), I am interested in increasing my QGIS know-how and contributing to the QGIS project. I want to know if this workaround (described below) is valid so that I can contribute by adding it in to the documentation such that other people will not spend time in finding it, like I did.

I want to run from a QGIS plugin a QgsTask created according to the documentation (https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/tasks.html). I tried both versions: a task from a function and I also created my own task by extending QgsTask class. In both cases, the task terminates successfully but the finished method is never  called/executed. I do not perform any GUI based operation from the run method as described in the documentation.

The workaround (found on the internet - https://gis.stackexchange.com/questions/296175/issues-with-qgstask-and-task-manager) was to to declare the task as global variable like:

globals()['task1'] = TestTask('waste cpu long', 20)
tsk_mngr = QgsApplication.taskManager()
tsk_mngr.addTask(globals()['task1'])

Furthermore, I can not longer use the QgsTask object (defined as above) if a task terminates. When I try to call any method of the terminated task I get the following error message "wrapped C/C++ object of type TestTask has been deleted". It looks like the C/C++ object was destroyed while the python object still exists. The workaround for this one is to catch the exception and to return something convenient (in my case for task1.status() return QgsTask.Terminated).

Any advice would be appreciated and I would like to thank all contributors to this super nice project.
​​​​​​
Wish you all the best!
Thanks,
Nicu 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210716/68a5cfbf/attachment.html>


More information about the QGIS-Developer mailing list