TIL | Pyo3 + Rust 연동 구조

타샤's 월드·2025년 9월 17일
0

Rust 연동 구조

  • 실제 러스트 모듈 초기 진입점은 rust/src/lib.rs 에 있는 #[pymodule(name="engine")]

  • 여기에 이런식으로 클래스 등을 추가

fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {

m.add("__version__", env!("CARGO_PKG_VERSION"))?;

m.add_class::<ml_v5::RRCFAnomalyPredictorV5>()?;

m.add_function(wrap_pyfunction!(util::gen_search_key_py, m)?)?;

Ok(())

}
  • 이걸 maturin으로 빌드해야함
  • 모듈명은 pyclass module에 정의된 대로 anomaly_engine.engine
[tool.maturin]

profile = "release"

module-name = "anomaly_engine.engine"

features = ["pyo3/abi3-py312"]

auditwheel = "repair"

manifest-path = "src/ml5/rust/Cargo.toml"

python-source = "src"

이렇게 하고 uv build 하면 src에서 anomaly_engine 디렉토리를 찾는 로그를 볼 수 있음.

anomaly_engine 디렉토리가 필요하고 그안에서 다음과 같이 임포트 가능

from anomaly_engine.engine import RRCFAnomalyPredictorV5
profile
그때 그때 꽂힌것 하는 개발블로그

0개의 댓글