Skip to content

Use real references to Graphics, proper garbage collection in ipython and jupyter #546

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

Merged
merged 9 commits into from
Jul 10, 2024

Conversation

kushalkolar
Copy link
Member

@kushalkolar kushalkolar commented Jul 1, 2024

closes #472, closes #161

also closes #393

No more weakreferences to Graphics! 🥳

Figured out how to properly garbage collect references proliferated by ipython in jupyter with some help from @lauraporta, thanks! 😄

The pygfx.WorldObject reference in Graphic is still a weakref.proxy, I thought let's keep it like that for now since we don't expect users to directly interact with the rendering engine objects much.

In PlotArea.delete_graphic() this is how proliferated references can be removed:

if IS_IPYTHON:
# remove any references that ipython might have made
ip = get_ipython()
# get all references in ipython namespace
references = list(ip.user_ns.keys())
for ref in references:
if ip.user_ns[ref] is graphic:
# we found the graphic, remove from ipython
ip.del_var(ref)
break

@kushalkolar kushalkolar requested a review from almarklein July 1, 2024 20:56
@kushalkolar kushalkolar requested a review from clewis7 as a code owner July 1, 2024 20:56
Copy link
Collaborator

@almarklein almarklein left a comment

Choose a reason for hiding this comment

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

No more weakreferences to Graphics! 🥳

🥳 ❤️

Comment on lines 647 to 651
for ref in references:
if ip.user_ns[ref] is graphic:
# we found the graphic, remove from ipython
ip.del_var(ref)
break
Copy link
Collaborator

Choose a reason for hiding this comment

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

How likely is it that the object is in user_ns? If it's only occasionally, can wrap this in:

if graphic in ip.user_ns.values():
    ...

I guess it's possible that the graphic is present more than once, under a different name?

Alt solution that might be faster because it avoids copying the keys and looking up the values:

            keys_to_delete = []
            for key, ob in ip.user_ns.items():
                if ob is graphic:
                    keys_to_delete.append(key)
            for key in keys_to_delete:
                    ip.del_var(key)

Copy link
Member Author

@kushalkolar kushalkolar Jul 8, 2024

Choose a reason for hiding this comment

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

How likely is it that the object is in user_ns? If it's only occasionally, can wrap this in:

if graphic in ip.user_ns.values():
    ...

in operators don't work if any item of the iterable is a numpy array, for example:

a = np.random.rand(10)
l = [a, "bah", 123]

# will raise a ValueError
123 in l

I've resorted to this, couldn't think of a more performance solution. Time taken scales with the number of items in the namespace.

if IS_IPYTHON:
    # remove any references that ipython might have made
    ip = get_ipython()

    # check both namespaces
    for namespace in [ip.user_ns, ip.user_ns_hidden]:
        # find the reference
        for ref, obj in namespace.items():
            if graphic is obj:
                # we found the reference, remove from ipython
                ip.del_var(ref)
                break

Copy link
Collaborator

Choose a reason for hiding this comment

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

No problems with deleting items from an object being iterated over? I guess it depends on IPython internals whether that'd be a problem.

clewis7
clewis7 previously approved these changes Jul 2, 2024
Copy link
Member

@clewis7 clewis7 left a comment

Choose a reason for hiding this comment

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

LGTM aside from the minor suggestions that Almar had

I think this will be super useful for users to get the actual object instead of a weakref back (less confusing when interacting with objects etc.). I agree that for now we just leave weakref to the world object...we can always change later.

@kushalkolar
Copy link
Member Author

ready for another look, sorry for the delay been sick 🤧

@almarklein
Copy link
Collaborator

Hope you get better soon!

@kushalkolar
Copy link
Member Author

@clewis7 ready to go!

@clewis7 clewis7 merged commit 9ff92a7 into main Jul 10, 2024
10 checks passed
@kushalkolar kushalkolar deleted the ipython-gc branch July 12, 2024 03:17
@kushalkolar kushalkolar mentioned this pull request Sep 29, 2024
5 tasks
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.

use of weakref.proxy GraphicCollection.__del__ method can cause confusing tracebacks Create a Proxy subclass that we use for our Graphic classes
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy