1

I have an api written in Rust but since a lot of colleagues only know Python I am trying to make it possible to build data pipelines in Python within the rust program.

The idea would be make a Python interface to the rust code and vice versa. Then from specific endpoints I would call a python module that could interact with the rust repositories etc.

A rough sketch of how I would like it to work (I am not too familiar yet with all the macros etc of pyo3 but I think the intent is clear):

Rust code:

struct DocumentRepository {
  database: postgres_database
}

#[pyclass]
impl DocumentRepository {
  #[pymethod]
  fn document(&self, name: String) -> Document {
     self.database.document(name)
  }
}

struct Application {
  document_repository: DocumentRepository
}

impl Application {
  get_document_in_python(&self, name: String) -> Document {
        let code = include_str!("../python_code");

        pyo3::prepare_freethreaded_python();
        Python::with_gil::<_, String>(|py| {
            let get_document: Py<PyAny> = PyModule::from_code(py, code, "example", "example")
            let document: Document = get_document.call(py, (), None).unwrap().extract(py).unwrap();
            document
        })
  }
}

Python code:

def get_document(name):
  // somehow interact with the rust application and get the document
  // return document

Since the application owns the connections to the database through the documentrepository (which is something I'd rather not change), the python code would need to interact with that specific instance of the struct to actually get the documents. Is this possible? Can I somehow pass a reference to the instance of the application struct and get the documents through this?

  • See [#283](https://github.com/PyO3/pyo3/issues/287). Everything you want accessible from Python you have to expose *explicitely* via `#[pyclass]`, `#[pyfunction]`, .... – Masklinn Jul 02 '23 at 11:30
  • Maybe I didn't explain clearly, but I don't mind exposing structs to Python. I am just unsure if I can have Python somehow interact with an instance of a struct, not the prototype of a struct. I have edited the question to clarify this. – Rutger Cappendijk Jul 03 '23 at 07:02
  • Sure, you're taking `&self`, operating on an instance. – Chayim Friedman Jul 03 '23 at 07:26

0 Answers0