From d26216aae88cf277b29996fc92f9b034cfb7333a Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Wed, 1 May 2024 14:08:34 +0200 Subject: [PATCH] Use pyo3::with_gil to call python --- .../extensions/ros2-bridge/python/src/lib.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/extensions/ros2-bridge/python/src/lib.rs b/libraries/extensions/ros2-bridge/python/src/lib.rs index a1a61727..73acfeaf 100644 --- a/libraries/extensions/ros2-bridge/python/src/lib.rs +++ b/libraries/extensions/ros2-bridge/python/src/lib.rs @@ -11,7 +11,7 @@ use arrow::{ pyarrow::{FromPyArrow, ToPyArrow}, }; use dora_ros2_bridge_msg_gen::types::Message; -use eyre::{eyre, Context, ContextCompat}; +use eyre::{eyre, Context, ContextCompat, Result}; use futures::{Stream, StreamExt}; use pyo3::{ prelude::{pyclass, pymethods}, @@ -54,14 +54,16 @@ pub struct Ros2Context { impl Ros2Context { /// Create a new context #[new] - pub fn new(py: Python, ros_paths: Option>) -> eyre::Result { - let warnings = py - .import("warnings") - .wrap_err("failed to import `warnings` module")?; - warnings + pub fn new(ros_paths: Option>) -> eyre::Result { + Python::with_gil(|py| -> Result<()> { + let warnings = py + .import("warnings") + .wrap_err("failed to import `warnings` module")?; + warnings .call_method1("warn", ("dora-rs ROS2 Bridge is unstable and may change at any point without it being considered a breaking change",)) .wrap_err("failed to call `warnings.warn` module")?; - + Ok(()) + })?; let ament_prefix_path = std::env::var("AMENT_PREFIX_PATH"); let empty = String::new();