Skip to menu

Robotics with Object Pascal

Rover written with RUST

Communication between Languages (IPC)

2025.11.06 00:22

me Views:29

You need to use a form of Inter-Process Communication (IPC).

Here are the best methods, from fastest to slowest:

 

  1. Shared Memory (Fastest, Most Complex):

    • How it works: Rust creates a block of shared memory. It gets the camera frame and writes the raw image bytes (e.g., as RGB data) directly into that memory. The Python program "attaches" to that same memory block and reads the bytes.

    • Pros: Blazing fast. It's "zero-copy," meaning the data is never copied; both programs are looking at the exact same data in RAM.

    • Cons: You have to manage synchronization yourself (e.g., using a "mutex" or "semaphore") so Python doesn't try to read an image while Rust is halfway through writing it.

    • Rust Crates: shared_memory.

    • Python Module: multiprocessing.shared_memory.

  2. Unix Sockets / TCP Sockets (Fast, Very Common):

    • How it works: The Rust program opens a (local) TCP or Unix socket and acts as a server. The Python program connects to it as a client. Rust grabs a frame, serializes its raw bytes (maybe with a simple header like "image length: 720, width: 1280, data..."), and sends it over the socket. Python reads the exact number of bytes, deserializes, and processes.

    • Pros: Much simpler to implement than shared memory. Still very fast. Flexible (can even run on different machines).

    • Cons: Involves one memory copy (Rust's memory -> kernel buffer -> Python's memory).

  3. Message Queues (e.g., ZeroMQ, Redis):

    • How it works: A more "industrial" version of sockets. Rust publishes frames to a "topic" (e.g., cam_frames). Python subscribes to that topic.

    • Pros: Extremely robust. Decouples the programs (Python doesn't care if Rust crashes and restarts).

    • Cons: Adds a third-party dependency (the ZMQ or Redis server).

For "almost real-time," Unix Sockets are probably the best balance of speed and simplicity.

 

===================================================

 

My Recommendation

Start with Method 1: Sockets (TCP).

 

It's 99% as fast as Shared Memory for this use case. While Sockets require the OS to copy the data in the kernel (unlike "zero-copy" shared memory), this overhead is tiny (microseconds) compared to the time it takes to run an AI model (milliseconds).

 

It is 1000% easier to implement and debug, avoiding the complex synchronization logic of shared memory. You will get a real-time (30-60fps) video feed from your Pascal app to your Python script with just a few lines of code in each.