You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>A video which shows that after pressing the button, the window can still be moved</p>
34
33
</video>
35
34
</div>
36
35
37
-
38
36
## How to Avoid Blocking the Main Loop
39
37
40
38
In order to avoid blocking the main loop, we can spawn a new task with [`gio::spawn_blocking`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/fn.spawn_blocking.html) and let the operation run on the thread pool.
@@ -56,7 +54,6 @@ This is not necessarily what we want.
56
54
</video>
57
55
</div>
58
56
59
-
60
57
> If you come from another language than Rust, you might be uncomfortable with the thought of running tasks in separate threads before even looking at other options.
61
58
> Luckily, Rust's safety guarantees allow you to stop worrying about the nasty bugs that concurrency tends to bring.
62
59
@@ -123,12 +120,12 @@ But why did we not do the same thing with our multithreaded example?
@@ -362,7 +359,6 @@ consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` cr
362
359
We could follow the advice directly, but the standard library also provides solutions for that.
363
360
With [`std::sync::OnceLock`](https://doc.rust-lang.org/stable/std/sync/struct.OnceLock.html) we can initialize the static with the const function `OnceLock::new()` and initialize it the first time our function `runtime` is called.
@@ -391,12 +387,12 @@ cargo remove tokio reqwest ashpd
391
387
392
388
How to find out whether you can spawn an `async` task on the `glib` main loop?
393
389
`glib` should be able to spawn the task when the called functions come from libraries that either:
390
+
394
391
- come from the `glib` ecosystem,
395
392
- don't depend on a runtime but only on the `futures` family of crates (`futures-io`, `futures-core` etc),
396
393
- depend on the `async-std` or `smol` runtimes, or
397
394
- have cargo features that let them depend on `async-std`/`smol` instead of `tokio`.
398
395
399
-
400
396
## Conclusion
401
397
402
398
You don't want to block the main thread long enough that it is noticeable by the user.
@@ -408,7 +404,7 @@ That means you have to run the task in a separate thread and let it send results
408
404
409
405
If your task is [IO bound](https://en.wikipedia.org/wiki/I/O_bound), the answer depends on the crates at your disposal and the type of work to be done.
410
406
411
-
-Light I/O work with functions from crates using `glib`, `smol`, `async-std` or the `futures` trait family can be spawned on the main loop. This way, you can often avoid synchronization via channels.
407
+
- Light I/O work with functions from crates using `glib`, `smol`, `async-std` or the `futures` trait family can be spawned on the main loop. This way, you can often avoid synchronization via channels.
412
408
- Heavy I/O work might still benefit from running in a separate thread / an async executor to avoid saturating the main loop. If you are unsure, benchmarking is advised.
413
409
414
410
If the best crate for the job relies on `tokio`, you will have to spawn it with the tokio runtime and communicate via channels.
0 commit comments