Closed pypt closed 3 years ago
Would it be possible to change your internal: Box<dyn Core>
to an Arc<dyn Core>
/ Rc<dyn Core>
and clone it before it's captured by the async move { }
? I noticed your register_worker
function only needs &self
not &mut self
, but if you need mutability maybe a Rc<RefCell<_>>
or Arc<Mutex<_>>
could help.
Thank you so much!
For the reference of others, I've wrapped internal
into an Arc
:
#[pyclass(name = "Core")]
struct WrappedCore {
pub(crate) internal: Arc<dyn Core>,
}
and cloned it before using it:
impl WrappedCore {
fn register_worker<'p>(&self, py: Python<'p>, config: WrappedWorkerConfig) -> PyResult<&'p PyAny> {
// Clone "internal" to be able to later use it within local_future_into_py()
let internal = self.internal.clone();
pyo3_asyncio::tokio::local_future_into_py(py, async move {
match internal.register_worker(config.internal).await {
Err(err) => Err(WorkerRegistrationError::new_err(format!(
"{}",
err.to_string()
))),
Ok(()) => {
Python::with_gil(|py| Ok(py.None()))
}
}
})
}
Hello! Thank you for a great module, and sorry for a noob question.
For my Python library, I'm wrapping a field of type
Box <dyn Core>
into a structWrappedCore
as such:WrappedCore
is supposed to be a Python-exposed wrapper around an underlying traitCore
which is defined as:I'm then trying to implement an async proxy method to
register_worker()
:but it doesn't compile because:
I've googled out that this is probably due to me trying to use
self
insidelocal_future_into_py()
. How can I accessself.internal
from within Tokio future definition?