Replies: 2 comments 4 replies
-
When you say "returning the data to Python", this probably implies you already have the GIL? |
Beta Was this translation helpful? Give feedback.
-
I think this really depends, but that said, it depends only in the same way as if you would implement that function in say pure Python. So if the return value should conceptually be a list and its only some callers which decide to treat it as an array for their purposes, so be it. If having a That said, turning a
As an aside, note that your code snippet does not really call "into Python". As a general remark, I would say that calling Python functions does always have a significant cost compared to calling a Rust function just because the calling convention is much more flexible and involved. But I think portability is not a concern if you were called from Python in the first place and want to "call back". As for complexity, there will also often be a cost due to marshalling arguments and return values as well as having a GIL token at hand, but PyO3 tries to make it as simple as possible. In my personal, experience calling back into Python code is not necessary very often, but when it was, that part of it was obvious. For example, because I wanted to make some part of a larger algorithm open to modification via the Python caller passing in a function which my code would call. But a lot of PyO3-written extensions end up converting their Python-passed arguments into "Rust-native" data structures, process them completely from within the Rust ecosystem and only convert the final results back into Python data structures. |
Beta Was this translation helpful? Give feedback.
-
This question is asked as a complete newcomer to
pyo3
and relatednumpy
handling.If converting a
rust
vec
structure into anumpy
array, e.g.Are there any drawbacks to calling Python from
rust
(complexity, performance, portability, etc)?In my case I am converting the
vec
to anumpy
array purely for purposes of returning the data to Python. It is not subsequently used fromrust
.In other words, would there be situations where it might be better just to return the
vec
structure to Python (as a list) and then take care of wrapping the list as anumpy
array from within Python directly?Thanks.
Beta Was this translation helpful? Give feedback.
All reactions