-
I have implemented an optimizer in Rust and use it from Python. I pass a Python function and use fn minimize(&self, py: Python, fun: PyObject) -> PyResult<OptimResult> {
let fun = fun.to_object(py);
let obj = move |x: &ArrayView2<f64>| -> Array2<f64> {
Python::with_gil(|py| {
let args = (x.to_owned().into_pyarray_bound(py),);
let res = fun.call1(py, args).unwrap();
let pyarray: &PyArray2<f64> = res.extract(py).unwrap();
pyarray.to_owned_array()
})
};
let optimizer = builder::optimize(obj).build();
let res = py.allow_threads(|| {
optimizer.run().expect("optimize the objective function")
});
... now I am trying to upgrade to the new API (PyO3 0.22), I've tried with the following code but I get a compilation error : fn minimize(&self, py: Python, fun: PyObject) -> PyResult<OptimResult> {
let fun = fun.bind(py);
let obj = move |x: &ArrayView2<f64>| -> Array2<f64> {
Python::with_gil(|py| {
let args = (x.to_owned().into_pyarray_bound(py),);
let res = fun.call1(args).unwrap();
let pyarray = res.downcast_into::<PyArray2<f64>>().unwrap();
pyarray.to_owned_array()
})
};
let optimizer= builder::optimize(obj).build();
let res = py.allow_threads(|| { # Compilation error `*mut pyo3::Python<'static>` cannot be shared between threads safely...
optimizer.run().expect("optimize the objective function")
});
... I've seen there are some developments in progress around |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
Thanks for your reply! I just had to put the
fun.bind(py)
call within the closure.