Hacker News new | ask | show | jobs
by ursuscamp 931 days ago
You can use tokio’s block_on to sync-ify. You need to instantiate a runtime, but you don’t need to do run your whole application in it, just the Future.

edit: Tokio can be beefy. You might look at some of the smaller single-threaded runtimes to execute your future in the main application thread if you’re only concerned about serial execution.

2 comments

Thanks. To further clarify, the SDK can be used from within a Tokio runtime or using Tokio's facilities in a synchronous runtime. Can other async runtimes be used? (The linked post seems to imply that they can.) It looks like Tokio gets installed as a dependency and I see the following when trying to use the futures package:

> thread 'main' panicked at /home/dev/.cargo/registry/src/index.crates.io-6f17d22bba15001f/aws-smithy-async-1.0.2/src/rt/sleep.rs:128:20: there is no reactor running, must be called from the context of a Tokio 1.x runtime

if you use other Async runtimes, you need to "wire them up", in this case by providing a "sleep" implementation. I'd strongly recommend using Tokio, especially if you're a beginner. I think the "beefy" statements are not necessarily accurate. You can use it as a single-threaded runtime if you want. Tokio is not going to have a significant impact on your compile times or binary size (given you're already using the SDK!)
I largely agree with this, honestly. Just use tokio
One thing I love about block_on is that it has a dedicated threadpool with a ton of threads. For async code, you want around as many threads as cores so the thread can run at full speed and have the scheduler handle switching, but for block_on, most of the time is sleeping, so the core can just switch between them all and take care of any that are done sleeping. Just don't use it for CPU intensive tasks.