Cancel BackgroundTask

Hi,

I’ve a quite long running BackgroundTask (moving very large files to AWS-S3) and I’m looking for a way to check if they are still running and also be able to cancel the BackgroundTask.
In my case the func are not async so it gets somehow passed to
await loop.run_in_executor(None, func, *args).

Is there a way to access the running BackgroundTask object?

Thanks

A background task is executed by calling its __call__ (which is async). The simplest way would be to subclass it and call something after await super().__call__() finishes.

thanks @uranusjr
but how can I cancle the BackgroundTask?

By “cancel” do you mean to stop it after it has started executing? There is no (good) way to do this, especially since your task is actually run in a thread (or process). Note that killing a thread is a problematic concept in general. You have to implement the interruption logic in func directly instead.

thank you for this feedback and the hint in the right direction!