feat: add spawn_blocking in std environment#10417
feat: add spawn_blocking in std environment#10417igiona wants to merge 1 commit intoslint-ui:masterfrom
Conversation
|
I guess this is where we're bordering with full async run-times. My feeling is that perhaps it's better to use a runtime's function such as https://docs.rs/smol/latest/smol/fn.unblock.html that implements thread-pooling, i.e. imposes an upper limit. OTOH I see the appeal of a simple solution inside Slint. Let's see what Olivier's take here is. |
I see your point. |
|
Thanks a lot for the PR and for explaining the motivation so clearly. I do not think this belongs in Slint itself. The functionality added here is not Slint specific. It is a general helper that can live in a separate crate, or some async helper crate Slint should stay focused on the UI and its own event loop. If anything, we could try to document how to use some other specific async runtime or other crate and how they play with Slint. |
|
I'm going to close this PR because we think it doesn't belong in Slint. You could make it a standalone crate or as part of a bigger crate. |
|
@ogoffart thanks for the feedback, and apologize for the delay, I have been pretty sick since a week. I was going to ask if you had such crate in mind, but you already answered ;) |
Lately I found myself dealing with large file dumps from my application.
The file-dump is triggered in a button callback, from within the slint event-loop.
I didn't want to build the "infrastructure" to pass my request to a tokio thread, there use the async file-syetem acceess and then go back to the slint event-loop with the action results.
I find that for this kind of operation, it's really practical to be able to:
Therefore, I added the possibility to run a blocking code into a newly spawned thread, allowing to
.awaitit's completion from within the slint event-loop (in aspawn_localfuture).This allows blocking operations (like large filesystem dump that for a reason or another aren't async) to not block the GUI while still being able to be awaited for proper results and error handling.
NOTE: I'm pretty sure that the code can't be merged as is due to lack of documentation and probably also the fact that this code has the chance to land in another module. Additionally, I'm not sure about the potential restrictions on using
std::sync::mutexfor example.I placed the code there, where it seemed more appropriate (I took
spawn_localas example), but since I'm not super familiar with the code base it might still be wrong.I'm seeking for advice on how to polish this up, and also of course if you see issues or better alternatives to this approach.