Craftrac
Craftrac

Reputation: 913

What is the best practice to follow in order to use Python to call functions from inside a Rust program?

I want to implement a system which consists of a Python UI and some Rust datastore and functions. The user executes the python code which in turn executes the Rust program in a subprocess. I want the Rust program to run in the background, waiting to exchange variables or function calls with the Python UI (for example every time a button is pressed).

My obstacles are:

  1. I have tried to make a simple Rust FFI library that can be invoked with ctypes from Python but I don't know how this library will be able to communicate with the compiled rust program that is running on the background.

  2. Another approach is to use PyO3 to make a python module using rust. But again I cannot understand how this will cooperate with the rust program to exchange data

Upvotes: 0

Views: 324

Answers (1)

Jmb
Jmb

Reputation: 23244

Your Rust background process will need to expose some kind of RPC mechanism such as jsonrpc or msgpack-rpc.

Then any other application can call into your background process using the same RPC mechanism. For example for Python, there are libraries for jsonrpc and msgpack-rpc.

Upvotes: 1

Related Questions