Is it possible to share the same #[pyclass]
instance as argument to python function in rust?
#3010
-
Assume created rust and python code like below: class UserDef:
def on_event(self, ctx:Context):
ctx.some_data += 9
u = UserDef()
c = Context(u)
Executor().run_backtest(c) #[pyclass]
struct Context {
pub some_data: i32
pub uesrdef: PyAny
}
#[pymethod]
impl Context {
#[new]
pub new(userdef: PyAny) -> Self {
Self {
some_data: 1,
userdef
}
}
pub fn add(&mut self) {
self.some_data += 111;
}
#[pyclass]
struct Executor {}
#[pymethod]
impl Executor {
pub fn run_backtest(&self, ctx: &mut Context) {
for i in 0..10000 {
ctx.add()
ctx.userdef.call_method1("on_event", ctx);
}
} I want to share the same |
Beta Was this translation helpful? Give feedback.
Answered by
adamreichold
Mar 5, 2023
Replies: 1 comment 2 replies
-
pub fn run_backtest(&self, ctx: &PyCell<Context>) {
for i in 0..10000 {
ctx.borrow_mut().add();
ctx.userdef.call_method1(intern!(ctx.py(), "on_event"), ctx);
}
} ? |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sun-rs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on_event
will want access to something like&PyAny
as it will not be able to handle the Rust typeContext
. So maybe something like?