8000 `Clone` (or alternative) on `GILOnceCell` · Issue #4428 · PyO3/pyo3 · GitHub
[go: up one dir, main page]

Skip to content
Clone (or alternative) on GILOnceCell #4428
@jakelishman

Description

@jakelishman

I have a pyclass struct that wants to contain a GILOnceCell<Py<PyAny>> for caching of a lazily constructed Python object, where the initialiser yields control to the Python interpreter so runs the risk of re-entrancy problems with regular OnceCell.

I'd originally tried to derive Clone on my struct (I'm still using py-clone for the time being, but working to disentangle our data structures), which if I understand correctly must fail for GILOnceCell because it requires the GIL to mediate both gets and sets. For the time being, I think the correct course of action for me for the clone is to manually GILOnceCell::new(other_cell.get(py).map(|ob| ob.clone_ref(py))) (or similar).

edit: if there's an A/B problem here, and there's something better I could be doing with this kind of cache, I'm totally open to alternatives to the answer being "that's not the right way to do this".

Would it be possible to either:

  • add an impl<T> GILOnceCell<Py<T>> { pub fn clone_ref(&self, py: Python); } method doing what I sketched?
  • give GILOnceCell the proposed derive macro for this kind of "clone with the GIL", if that is what comes out of Add trait for cloning while the GIL is held #4133?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0