Passing Python function to different Rust thread #3788
-
Hey all, I'm trying to adapt a Rust API for Python using PyO3 but I've hit a bit of a roadblock with threading. I have an existing Rust API that looks something like this: pub fn run_experiment<F>(&mut self, window_options: &WindowOptions, experiment_fn: F) -> ()
where
F: FnOnce(Window) -> Result<(), SomeErrorType>
+ 'static
+ Send, This function uses fn run_experiment(
&mut self,
window_options: &PyWindowOptions,
experiment_fn: &PyFunction,
) {
// create rust FnOnce(Window) -> Result<(), SomeErrorType> + 'static + Send,
let rust_experiment_fn = move |window: Window| -> Result<
(),
SomeErrorType,
> {
experiment_fn.call0(); // call without args for now
Ok(())
};
// run the experiment
self.0.run_experiment(
&window_options.0,
rust_experiment_fn,
);
} But that gives me a " |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
You'll have to use GIL-independent objects, i.e. |
Beta Was this translation helpful? Give feedback.
You'll have to use GIL-independent objects, i.e.
experiment_fn: Py<PyFunction>
. Inrust_experiment_fn
, you then have to acquire the GIL before you can callexperiment_fn
.