Skip to content
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

Enable cross-interpreter sharing of tuples #111623

Closed
tonybaloney opened this issue Nov 2, 2023 · 3 comments · Fixed by #111628
Closed

Enable cross-interpreter sharing of tuples #111623

tonybaloney opened this issue Nov 2, 2023 · 3 comments · Fixed by #111628
Labels
topic-subinterpreters type-feature A feature request or enhancement

Comments

@tonybaloney
Copy link
Contributor

tonybaloney commented Nov 2, 2023

Feature or enhancement

Proposal:

This issue is for adding support of sharing tuples (and tuples of tuples) through the crossinterp API and the interpreters module when that arrives.

The data structure will have to have a variable length.

struct _shared_tuple_data {
    Py_ssize_t len;
    _PyCrossInterpreterData **data;
};

It will use the xid registry to lookup the type values in the tuple, then encode them using the crossinterpdatafunc callbacks. This should work recursively if the tuple contains a tuple.

PR to follow.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@a-reich
Copy link

a-reich commented Dec 22, 2023

Quick question @tonybaloney , is is_shareable intended to be reliable for containers? It now returns True for tuples even if they contain non-shareable types.

@ericsnowcurrently
Copy link
Member

Excellent question! The underlying machinery for shareable objects isn't very sophisticated currently. is_shareable() basically just checks if the object's type supports sharing. To make it more accurate, as you've implied, we would need either to try to actually share the object (like how the builtin hasattr() works) or to add a hook that each type may implement to indicate if an object is shareable.

FWIW, the relevant code for the (currently internal-only) shareable-type protocol is in Include/internal/pycore_crossinterp.h and Python/crossinterp.c. For the is_shareable() implementation look in Modules/_xxsubinterpretersmodule.c. Any changes would happen in those files.

Keep in mind that, right now, the machinery for shareable objects is implemented with a global registry of types but I plan on writing a PEP in the near future targeting 3.13 to add a dedicated field to PyTypeObject to replace the registry. Also note that is_shareable() is not part of PEP 734, though we may add it to the module (not the PEP) later.

@a-reich
Copy link

a-reich commented Dec 23, 2023

Thank you for the explanation on the current design! Yeah, it makes sense that is_shareable is not very complex right now, and that it's not part of the official spec in PEP 734. (I just saw it in test.support.interpreters and was wondering how to use it when experimenting with the stuff there). The idea of replacing the registry with a type field/protocol sounds interesting for sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-subinterpreters type-feature A feature request or enhancement
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants