Re:Calling Rust from Python
by xwowsersx on Sunday October 08, 2023 @03:38PM
(#37813722)
Just looking at the function signatures there:
is the idea that the `Python` type represents the GIL and you're actually acquiring it from within your Rust application?
[ Parent
| on HN ]
Re:Calling Rust from Python
by guitarbill on Sunday October 08, 2023 @03:51PM
(#37813833)
In the example, it isn't used, so AFAIK doesn't acquire the GIL. But that type is used to acquire the GIL explicitly or e.g. import modules.
The Python<'_> type appearing as a parameter means the caller has already acquired the GIL for you. The GIL is held when Python calls into extension modules, and PyO3 doesn't automatically release it (but you can do so yourself).
It's essentially a dummy parameter that serves as proof that the GIL is held. You need to provide it to PyO3 when calling back into the Python runtime, that way the compiler ensures the GIL is still held where required.