[QGIS-Developer] Properly terminating a QgsTask?

Benjamin Jakimow benjamin.jakimow at geo.hu-berlin.de
Mon Jun 5 04:27:16 PDT 2023


Hi Johannes,

just stop the execution in QgsTask::run(), for example using a break 
that stops a loop, and return False instead of True.

 From https://api.qgis.org/api/3.30/classQgsTask.html


> Performs the task's operation.
>
> This method will be called when the task commences (ie via calling 
> start() ), and subclasses should implement the operation they wish to 
> perform in the background within this method.
>
> A task must return a boolean value to indicate whether the task was 
> completed successfully or terminated before completion.
>
> virtual bool QgsTask::run 	( 	
> 	) 	
>
> 	protectedpure virtual
>
Greetings, Benjamin

Am 02.06.2023 um 14:19 schrieb Johannes Kröger (WhereGroup) via 
QGIS-Developer:
> Hi,
>
> https://qgis.org/pyqgis/3.30/core/QgsTask.html says
>
> > Long running tasks should periodically check the isCanceled() flag 
> to detect if the task has been canceled via some external event. If 
> this flag is True then the task should clean up and terminate at the 
> earliest possible convenience.
>
> How are we supposed to "terminate" a task properly?
>
> If we check for task.isCanceled() (or call task.cancel() ourselves for 
> testing) and simply return:
>
> - QGIS will still show a notification that the "Task completed" 
> (instead of "Task failed") 
> (https://github.com/qgis/QGIS/blob/d3d0431685883b837caa571ad7282828cfbde65e/src/app/qgisapp.cpp#L15707)
>
> - The taskTerminated signal is not emitted. But the taskCompleted 
> signal is emitted.
>
> - The task's status is QgsTask.Complete
>
> There is no obvious way to "terminate" the task from the Python side. 
> We cannot set the task's status attribute to QgsTask.Terminated 
> (https://qgis.org/pyqgis/3.30/core/QgsTask.html#qgis.core.QgsTask.Terminated).
>
>
> If an exception happens in the task then it seems to terminate 
> "properly", i.e. its status will be QgsTask.Terminated, the 
> taskTerminated signal is emitted and a notification "Task failed" is 
> shown. This is the only way I found today to get into this state. I 
> have not found this documented and only by accident...
>
>
> Example for a QgsTask.fromFuncction(..., flags=QgsTask.CanCancel), 
> uncomment one of the final lines:
>
> def foo(task):
>         time.sleep(6)  # must be >= minTaskLengthForSystemNotification 
> (5 by default)
>         #task.cancel(); return  # -> Task complete
>         #raise Exception("fail plz")  # -> Task failed
>
>
> It feels like a bug to me (either task.cancel should directly set its 
> status to QgsTask.Terminated or we should have a task.setStatus() 
> method) but everything in and around QgsTask is fairly opaque and hard 
> to grasp so maybe I am just missing something obvious.
>
> Cheers, Hannes
>
>
> PS: I did not test the example function in a clean environment, in my 
> test cases I have code like this as methods of a class.
>
-- 
Dr. Benjamin Jakimow
Earth Observation Lab | Geography Department | Humboldt-Universität zu Berlin

e-mail:benjamin.jakimow at geo.hu-berlin.de
web:https://hu-berlin.de/eo-lab
phone:  +49 (0) 30 2093 6846
mobile: +49 (0) 157 5656 8477
fax:    +49 (0) 30 2093 6848
mail:   Unter den Linden 6 | 10099 Berlin | Germany
room: 2'211
linkedin:https://www.linkedin.com/in/benjamin-jakimow
matrix: @jakimowb:hu-berlin.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230605/529bf2f0/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4745 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230605/529bf2f0/attachment.bin>


More information about the QGIS-Developer mailing list