How are background tasks prevented from being garbage collected? #338
Unanswered
jakubsvehla
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
In the official asyncio documentation, I read that you have to keep a strong reference of a task in order for it not to be garbage collected. However, I noticed that the
current_app.add_background_task
method adds the resulting task to a weak set so it seems that it only keeps a weak reference and it does not return the task so I cannot keep a strong reference myself. But I have tested it and it seems that there are still some strong references until the task finishes which keeps it from being collected. How does it work? Where are the strong reference kept?Can I rely on the task not being collected before it finishes running?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions