-
Notifications
You must be signed in to change notification settings - Fork 777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to define a struct without exposing pyo3_GilGuard to user ? #1808
Comments
Usually the solution to this kind of problem in Rust is to push control of the lifetime to the user. So instead of acquiring the GIL inside your function, ask the user to acquire and pass the
|
If you want to produce a struct holding a Python object(s), you probably want this: struct Foo{
field: Py<...>
} Note that both your structs as written are bound to the |
@mejrs , I want to create a struct, that user would not need to ever realize it is something with python. So the solution @davidhewitt said is not what I want. What I want is like this:
As the code above , field With the lifetime issue, I've also googled to check if lifetime of a field could be related to another field of the same struct---unfortunately, it seems this is not supported in rust. |
How about not storing the GIL at all, and instead using Unless you're doing something in a tight loop which would lead to lots of GIL locking & unlocking, this would also give your users the flexibility to multithread. If you create a struct like you're attempting above, you'll end up locking the Python code to the same thread as this struct. If you really need to store the GIL token in the struct (and lock it to a single thread), then store
|
@davidhewitt , the solution you recommended may be similar to what I've done with In my project, there is only one struct need to deal with python. And also unfortunately, just like you mentioned, it would have to loop quite a long time with python(maybe more than 1,000,000 steps). So, with the original solution I mentioned(the very 1st post), it would be both efficent and freindely enough to struct user, if there is no exposure of |
I think there's a misunderstanding here: it's not possible to have more than one GIL. If your struct holds a This is why I suggest making you user pass |
@davidhewitt ,then how to launch multi python interpreter processes with pyo3? As far as I know, |
Storing the gilguard in a struct doesn't really strike me as idiomatic. Is there a good use for it? I don't see people storing We can deprecate it (or add something like "this will be deprecated in the future") and see what pops up. |
Pyo3 doesn't support running different subprocesses like Python's |
@mejrs , I've checked the paralellism chapter before, but I am still a little confused. That |
There is no "paralellism among python threads". It can't exist. |
Please explain what you mean by that. |
@birkenfeld , I want to call a python function in paralellism with rust just like with pure python code--- In python , due to GIL, multi-process has to be used to do so, instead module |
Well, that should be doable, as long as only one Rust thread (at a time) tries to acquire the GIL, you can have true parallelism between the Rust threads and the thread running Python. |
@birkenfeld , so I think this is the confusing part. Refering to " true parallelism", you mean threads between rust and python, but not threads between different python processes, right? The latter is what I need . |
If by "processes" you don't mean processes but threads, correct. PyO3 cannot disable/work around the GIL for you, sadly. |
@birkenfeld , sorry for the mix using of "process" and "thread". |
No. As I said, only one Rust thread can call into Python at a time. Rust/PyO3 does not allow you to get around the GIL. |
To add to @birkenfeld's answer: No, only one thread running Python code can ever make progress. That's the whole design of the GIL. Think of it as a high contention If you want what you call "true parallelism like However, I must then ask why not just use Looping this back to the original question, this is why we don't recommend storing If you want to have multiple Rust threads running in parallel, the right design for interacting with Python is to use If using |
@davidhewitt , of course I don't ever want to think of Besides, after discussion, now I know it is impossible to make the python code run in true parrallism with In addition, though using |
Just as a follow-up you may want to watch #576, however this is a far-off future feature still. |
@davidhewitt , thanks for the link, and hopefully, it coulde be one of pyo3 feature, also may become an advantage over other similar crates. |
@mejrs, personally, I had been sharing an To answer your question, the difference here is that from a |
@LegionMammal978 , thanks for reply. Do you mean that And different from what you did in your project, since I don't want extra overhead, I don't use |
win10
At present, I could define a struct with pyo3 like this:
And what I want is like this:
But due to the lifetime issue, I can not do this. Is there a workaround?
The text was updated successfully, but these errors were encountered: