Skip to content

Most efficient way of returning large byte array from Rust to Python #1385

Closed
@cswinter

Description

@cswinter

I'm looking for some guidance on the best way to return a large byte array to Python code and/or share access to raw memory between Rust and Python.

Basically, my application transfers large amounts of data over the network in Rust and then hands the data over to Python (but in some cases I still want concurrent access to the data on the Rust side as well).
One of my ideas is to allocate a numpy array and have the networking code write into it directly, though in that case I don't think the Rust code can still safely access the array because it might be modified concurrently (related question, if I store a reference to a numpy array object on the Rust side, will GC work properly or could the numpy array get deallocated once all references on the Python side disappear?).

I think PyBytes::from_ptr might be what I want but I haven't found any documentation on what invariants it expects to be maintained without triggering unsafety.
If I have a Vec<u8> on the Rust side and return direct access to it with PyBytes::from_ptr, I assume this is fine as long as I don't deallocate the Vec and the Python code doesn't do anything funky? Is there way to ever safely/automatically deallocate the Vec, or would that require something like exposing a free method that has to be called explicitly by the Python code after it stops accessing the bytes object?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions