Skip to content

Fixed 1656: Animation is Garbage Collected if not explicitly stored #8214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

raychut
Copy link

@raychut raychut commented Mar 7, 2017

Fixed #1656 where the animation object somehow gets deleted when not assigned a variable to it. Had animation.py store it as an attribute of the figure as suggested so that user behavior doesn't change

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Mar 7, 2017
@@ -874,6 +874,12 @@ def __init__(self, fig, event_source=None, blit=False):
if self._blit:
self._setup_blit()

# Saves the animation is the figure class to prevent Python's garbage
# collection from deleteing the animation.
if (not hasattr(fig, 'animations')):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to do this, add it in the __init__ instead of using a hasattr check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the animations attribute in figure init instead of the hasattr.

try:
length = len(fig.animations)
assert(length != 0)
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the explicit try..except adds much here, just the bare assert is good enough in both places.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try..except has been removed!

@tacaswell
Copy link
Member

@raychut Thank you for your work on this!

I am concerned about this for exactly the reasons that @dopplershift listed in the original issue (creates circular coupling).

If we do this for animation, we should also do this for widgets. Maybe instead of animations it should be interactives or something like that?

@anntzer
Copy link
Contributor

anntzer commented Mar 7, 2017

I would just stick everything in a private _keep_alives until we can decide on something better.

@raychut
Copy link
Author

raychut commented Mar 7, 2017

@tacaswell @anntzer I personally agree in changing the name into a private function and/or change to a more generic variable name to store other interactives.

@dopplershift
Copy link
Contributor

So I like the suggestion about _keep_alives. But one quandry in general here. After this patch, we now have:

fig = plt.figure()
anim = matplotlib.animation.ArtistAnimation(...)
del anim  # Animation resources not released due to reference in figure

So we've traded one confusing behavior where resources are being freed when not expected with another where resources aren't freed. I'm not sure what to do...

@raychut
Copy link
Author

raychut commented Mar 9, 2017

@dopplershift I've changed the attribute name from animations to _keep_alives.

That is a tricky point and I guess it is the consequences from not changing the API. I'm not sure what to do either....however even before this change using del to delete an object seems like a very unnatural thing to do too.

@dopplershift
Copy link
Contributor

@raychut Python's del behavior can be a little weird--but it is the standard way in the language to delete an object in Python, similar to delete in C++.

I'm curious if any of the other matplotlib devs would like to weigh in on our conundrum here.

@anntzer
Copy link
Contributor

anntzer commented Apr 12, 2017

@dopplershift's argument in #1656 ("I still don't consider this a problem, since FuncAnimation and co. are classes; nowhere in Python do we create an instance of a class and expect it to go on living if we don't assign a name to it.") looks reasonable to me, so count me as -1 on this change (i.e. let's leave things as they are)... as of now.

@WeatherGod
Copy link
Member

WeatherGod commented Apr 12, 2017 via email

@tacaswell tacaswell dismissed their stale review April 12, 2017 13:55

outdated

@tacaswell
Copy link
Member

I am still torn on this. On one hand, it does 'fix' a common mistake, on the other hand it creates one more circular reference, one more implicit bit of 'magic', and it feels like we are working against the language.

Directly calling del might not be common, but is also what happens when things go out of scope.

This issue is now documented clearly, maybe it also needs a call-out box? http://matplotlib.org/api/animation_api.html?highlight=animation#animation

@anntzer
Copy link
Contributor

anntzer commented Apr 12, 2017

it is more pythonic to have modular animations

why?

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Aug 29, 2017
@jklymak
Copy link
Member

jklymak commented Jul 16, 2019

Closing as stale, and not quite reaching consensus... Thanks for the contribution!

@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Animations & Object Persistence
7 participants