2

I maintain a big repo written in Rust, which involves certain functionalities such as I/O with databases, and communications with other nodes using Websocket.

Now I need to allow other people to write Python scripts which can use DB I/O and websocket stuff, which is written in Rust as mentioned above. Looks like I have 2 possible approaches:

  1. One way is to compile those Rust-written stuff into shared libraries (.so files on linux), and then load them as with Ctypes in Python.
  2. Another way is to build a "sidecar" written in Rust which has these DB and Websocket functionalities, run the sidecar as a daemon process and let the Python script interact with this daemon process with IPC (e.g., socket).

I feel there are pros / cons in each approach. For the first approach, dealing with either Rust ffi or Python Ctypes is probably not very straightforward. The second approach might have a little bit overhead in latency.

Nowadays I am seeing more and more adoption of the 2nd approach (a.k.a. sidecar pattern), while the 1st approach sounds more old-schooled. I want to post my question here, and would appreciate any comments regarding this!

Xiaoya Wei
  • 21
  • 1
  • 2
  • 1
    I would create a well defined protocol and make idiomatic API entry points in both languages and use Unix sockets to avoid networking overhead – MeetTitan Jan 27 '22 at 16:38
  • 4
    You could also use PyO3 to do option 1 without having to do ctypes: https://pyo3.rs/v0.15.1/ – lkolbly Jan 27 '22 at 17:07

0 Answers0