[QGIS-Developer] Questions about using QgsTask class

Julien Cabieces julien.cabieces at oslandia.com
Mon Jul 26 01:30:36 PDT 2021


Hi Nicolae,

It's not mandatory to add the task to the globals, you just need to keep
a reference to your QgsTask somewhere (as a member of another class for
instance) during the task execution. If not, Python garbage collector
will delete the referenced object when the variable scope ends (when the
methods which create the task ends), which would lead to some errors.

Regarding the second point, it's important to understand that the
QgsTaskManager will destroy your task when it finishes, so your variable
referencing your task is destroyed (actually the python object is
not but the underlying C++ object is, so the message "wrapped C/C++
object of type TestTask has been deleted").

You can connect to signal taskCompleted, get everything you want from
your task, and then it will be deleted.

Kind regards,
Julien

> 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 
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer



More information about the QGIS-Developer mailing list