Replies: 11 comments
-
You should change your Player struct to: #[pyclass]
pub struct Player {
acc: Py<Acc>,
context: Py<Context>,
} This way the |
Beta Was this translation helpful? Give feedback.
-
Thanks for reply. If method of 'Py‹Account›' need to be called in rust, I must aquire gil first then call the method? If so, the performance would be worse than pure rust method of origin struct? |
Beta Was this translation helpful? Give feedback.
-
Yes, but in your example you already have the GIL when entering #[pymethods]
impl Player {
pub fn run(&mut self, num:i64, py: Python) {
for _i in 0..num {
self.context.on_calculation(&self.acc, py);
}
self.acc.as_ref(py).clear_cash() // Account need to be used in Rust !!!!
}
} |
Beta Was this translation helpful? Give feedback.
-
Could I make this conclusion that if I want to fully achieve pure rust performance I must make sure no 'acquire gil' in my rust code of the extension module? |
Beta Was this translation helpful? Give feedback.
-
This is probably not the definite answer you want to hear, but when assessing the performance of software, then you just need to measure. Whether taking the GIL is problem or not is hard to say in a general manner and without additional context. There can even be places where taking the GIL can advantageous, e.g. if separate synchronization mechanisms would be required otherwise. In your particular case, |
Beta Was this translation helpful? Give feedback.
-
Thanks! I understand that. What I want to achieve is to let the flexible part be written in Python while the fixed part executed in Rust. Some people use Python+ctypes+cpp to do the same stuff like what I did.But they have a trick that they can use cython to re-write their Python codes easily with its 'nongil' mode, so they could get a better performance than a for-loop with gil. Could we do some thing like that? |
Beta Was this translation helpful? Give feedback.
-
We've been talking about making a The draft PR is #1979 Perhaps you'd be interested in helping test and finish off the functionality? Hopefully we can get it complete for the 0.17 release. cc @mejrs |
Beta Was this translation helpful? Give feedback.
-
It seems that immutable pyclass is very suitable for read data which is read-only? In fact I understand that if we call a method in Python we must get gil first, but what I do confused is that if 'Acc' is write in pure Rust,why couldn't we call its methods and change value directly while skip gil action? At the beginning, the reason I wtire the example is to expose less to the Python side and let 'Player' do some manipulate work on 'Acc' so I wouldn't need to write much in Python side. But if I must use gil to call acc in Player ,it seems make no sense to put it in Rust side? |
Beta Was this translation helpful? Give feedback.
-
The point is that in If |
Beta Was this translation helpful? Give feedback.
-
Thank you very much! I'm fully understand this mechanism. When access some vector or array type data, people may use |
Beta Was this translation helpful? Give feedback.
-
To put it another way, what Rust really cares about preventing is aliased mutability, which has been a big problem in language design. But if there is no aliasing (by, for example using a Mutex or RefCell, or in this case PyCell), or if there is no mutation (because the object is immutable) there are no problems. |
Beta Was this translation helpful? Give feedback.
-
Because Python for-loop is slow, I use
Pyo3
to run a loop and manipulate some struct(contains Python class with methods).To do that I have three struct-
Player
(Pure Rust)Account
(Pure Rust)Context
(empty in Rust and user define its real code in Python),Player
hasAccount
andContext
in it, it will loop the method ofContext
.While in each loop, it would send its
Account
instance as&mut self.acc
argument into itsContext
instance. BecauseAccount
would contains a lot of data, and it would be also use in Rust side and Python side. I couldn't copy it everytime.So here is the question: what should I do to
self.acc
inPlayer
struct to make it could be used as argument in Rust and Python at sametime without copy? Or say how to send apyclass
as an argument to aPyAny
object and call ites method?Python code:
Beta Was this translation helpful? Give feedback.
All reactions