Store task information in BackgroundTask object #1816
Unanswered
Harry-Lees
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The other day I was working on an application with a background task where I wanted to know the status of the background task after it'd been started. This is quite a common thing that users may want to do:
I've added a minimal example below (although it turned out to be a bit long, sorry).
In the above example, there are several HTTP endpoints relating to a single long running task. There are several drawbacks to this approach. (Please correct me if I'm wrong about anything here)
The common response (and almost certainly better design in the long run) to this problem would be to use a 3rd party message broker to handle background tasks (Celery, Redis, etc). However, for smaller projects, or in circumstances where having your task complete 100% of the time isn't that important, having a separate task manager running might be a bit overkill, and I think this would be a helpful addition in those circumstances.
I've included an example implementation below:
Using the modified BackgroundTask above, the code could be changed to:
(some details omitted for brevity)
Obviously, this doesn't fix all of the issues with this approach, and there are (lots of) circumstances where using an external task manager is a better option, however, as I mentioned above, I think this could be helpful for smaller projects where a 3rd party task manager would be a hassle to setup.
Beta Was this translation helpful? Give feedback.
All reactions