Repository navigation
Is there any method to get GIL in async? #1912
|
Is there any method to get GIL in async? It is useful when users call python from rust async fn run_python() {
let gil = Python::acquire_gil().await;
let py = gil.python();
do_something();
}
#[async_std::main]
async fn main() {
run_python().await;
} |
Replies: 8 comments
|
I'm afraid |
|
@davidhewitt Maybe it is feasible to do so, although many specific details are not considered. |
|
I would strongly advise not using that. In particular: This causes two waits: an async wait to acquire the If you really want a true |
|
What I want to express is actually: blocking APIs will not block when they meet certain conditions. Therefore, as long as the asynchronous primitives are used to ensure that the blocking APIs will not be blocked when accessed, the blocking APIs can be converted to asynchronous APIs. My example is written under your premise " the GIL is like a sync Mutex", that is, through asynchronous Mutex, it is guaranteed that there is only one coroutine that calls My English is not very good. These sentences are largely translated by Google. If there is any offense in the words, please understand, it is not my intention. |
|
Perhaps in this case you are better off having only one thread that acquires the gil and use channels to communicate between threads, if your program is multithreaded. Generally it is recommended that things like resources work better with message passing than passing around a mutex containing one. |
|
@mejrs So at least we have reached a consensus: it is feasible to obtain GIL asynchronously. Why not try to support it in pyo3? |
@Easonzero I understand your point however I must disagree still with the implementation. Any number of other threads spawned from Python by For this reason I think offering this API in PyO3 at the moment is misleading to users - if I use an In your own code you are able to control what other threads may be spawned, so I'm fine for you to do this in your own code if you choose.
Please do understand that I very much want to have I still believe that an async |
That's not what I said. |
@Easonzero I understand your point however I must disagree still with the implementation. Any number of other threads spawned from Python by
threading.Thread, or even Rust threads which are not running async code, may also be waiting onacquire_gil. So I expect it to be very likely that calls toacquire_gilwill block even with the companion asyncMutex.For this reason I think offering this API in PyO3 at the moment is misleading to users - if I use an
asyncfunction from a library I expect it to be guaranteed that the function will never block.In your own code you are able to control w…