feat: add spawn_blocking in std environment #10417
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.