5 ms·
I've become worried about Rust as "async" worms its way into more crates. "Async" is optimized for I/O bound programs. The special case of a large number of slo
by Animats 5y ago
I've become worried about Rust as "async" worms its way into more crates. "Async" is optimized for I/O bound programs. The special case of a large number of slow network connections talking to a server, web services, is what it's really used for. If you have multiple compute-bound tasks crunching away, it's a mismatch.
- jollybean 5y agoAsync is also used in UI programs with intermittent, event based interactivity. That's 'front end and back-end' ... probably the bulk of software right there. A bunch of threads running 'compute bound tasks' is probably a special case.
- zekica 5y agoI don't think they are mutually exclusive. You can use async and threads at the same time.
- lmm 5y agoYou can't really in rust, because it doesn't let you abstract over async-vs-not. If your function does something that a caller might want to happen async, it has to make a choice about whether to do it in an async way and return future (cumbersome and inefficient to use from blocking thread-based applications), or do it in a blocking way (unsuitable for use from async applications).
- Arnavion 5y agoFunction is async and you want to run it synchronously? Create an async runtime and use its equivalent of `runtime.block_on(async_fn())` to drive the future to completion synchronously. Function is blocking and you want to use it in an async context without blocking the async worker thread? Use your runtime's equivalent of `spawn_blocking(|| sync_fn()).await` that will run the function call in a threadpool dedicated for blocking tasks. (Both those examples are for tokio.)
- lmm 5y agoRight, and in both cases you end up being inefficient and using more threads than you wanted to (and in the second case, potentially undermine the whole point of using async at all).
- Arnavion 5y ago>in both cases you end up being inefficient and using more threads than you wanted to There are no extra threads in the former case. >(and in the second case, potentially undermine the whole point of using async at all) If code is blocking for long periods of time, it shouldn't be running on the async worker thread in the first place. Marking such a function `async` would be wrong. If I write a tight loop that takes seconds to do its job, putting an `async` on the function is worse than not having it. Spawning such work in a different thread is correct. There's no "whole point of using async" that is being "undermined".
- lmm 5y ago> If code is blocking for long periods of time, it shouldn't be running on the async worker thread in the first place. Marking such a function `async` would be wrong. Right, you need to go right down and e.g. use a non-blocking I/O primitive instead of a blocking I/O primitive. Rust gives you no way to polymorphically do one or the other.
- andrewzah 5y agoSupporting async isn’t a bad thing. It is annoying when working with async stuff and suddenly a library doesn’t support futures or streams. But I do agree that it seems that people jump to use async instead of e.g. threads. I think there should be more literature around what situations work best for threads vs async, etc.
- dodobirdlord 5y agoI think in a lot of cases the async programming model is easier to work with than the thread programming model. In a sense it’s declarative vs procedural programming over again. With threads you have to identify parallelism opportunities at write time, and if you plan poorly you can end up with low core efficiency. With async programming you break your execution up into a graph of inputs and outputs and let your async scheduler dynamically bin-pack everything, paying an overhead for consistently high core efficiency without having to think about it or change your code very much.
- kevincox 5y agoI always thought this, but people don't seem to find the convenience of async as a strong selling point over threads. There isn't a fundamental reason, but doing something like: thing1_promise = get_thing(1) thing2_promise = get_thing(2) thing1 = thing1_promise.await thing2 = thing1_promise.await is almost always more difficult to do with threads. Sure, some threading APIs support joining nicely but it tends to be more awkward. Especially if you want to do something like handle the first request to finish first. Also the startup cost of threads affects how you write code. Does your library spawn a new thread for every HTTP request? Probably not, it is expensive and the server may be on the same host. So you end up with less consistent async support. Since async programming is a lot cheaper you tend to see better support which adds up.
- nandhinianand 5y agoThat's a very intriguing metaphor.. and reminds me of the optimization manually vs using `gcc -O2` .. I am wondering if the "pre-mature optimization is the root of evil" quote applies here and we should all just use async first and only go for threaded/multi-processing programming if there seems to be a performance issue for the business application.
- thesz 5y agoDepends on the compute tasks. Even OpenMP has "dynamic" scheduling pragma for loops, in the case each loop iteration takes different time to complete. "async" is a way to address that, sorta.
- berkut 5y agoYeah, it may just be me, but I was looking into WebDriver crates yesterday to try and control browser engines, and there are non-async ones, but async ones seem more common (at least in terms of blog posts talking about them). The examples they give to use the API using async, I really don't understand, and I can only assume I'm missing something: the example steps of going to a URL, finding a resultant element, entering text, then clicking the search button, then finding another element and then capturing a .png snapshot image for the result are in my mind all sequential, and depend on the previous step: I really struggle to understand how async helps in this case (at least the basic one), as I don't see how any of those steps can be parallelised.
- Arnavion 5y agoThe fact that they're async doesn't mean you're expected to be able to parallelize the calls, just that you can do something else while waiting for a response if you want. They involve IPC to another process which can take its own arbitrary amount of time to return a response. There's no reason to block your program during that period. I don't know anything about webdriver's API specifically, but as a general example, "do something else while waiting for a response" can include "stop waiting for a response after a timeout expires". Maybe you want to set a timeout on the "fetch a URL" function call. You can't do that with a synchronous function unless the function internally supports a timeout. With an asynchronous function you can just race it with a timeout future, and if the timeout future completes first you can stop waiting for the fetch future and fail your test.
- jgraham 5y ago(I work on WebDriver standard / implementation) Classic WebDriver, when used to control a single browser is indeed a synchronous API. So for example the server-side WebDriver implementation used in geckodriver is fully synchonous; it accepts a HTTP request, and does a bunch of blocking work to execute that command in Firefox. But there are a couple of reasons you might want an async client, even though the browser side is synchronous: * You might want to drive multiple browsers at the same time. For example when running multiple tests in parallel you would have multiple concurrent requests going to different browser instances. Obviously you could implement that as multiple threads/processes/etc. but if async has the ergonomics you're looking for it is certainly a viable option. * Modern extensions to WebDriver don't follow the command/response model. Many clients have additional functionality that's not implemented on top of WebDriver, but using browsers' built in debugging protocol. Even Selenium these days uses the Chrome Debug Protocol for some features (e.g. access to logs). Those protocols are event based and so not suitable for a fully blocking API. Of course one could still design something that doesn't require async for those use cases, but I think it's generally believed that async offers the best APIs in languages that support it.
- pjmlp 5y agoThat is why on .NET while working on existing code I tend to add some Task.Run() calls. Async/await as concept taints the call stack all the way up to main and is unmanageble on existing code. While working on C++/WinRT I am seeing the same problematic showing up, so no wonder that Rust ecosystem is now facing the same issues.
- Arnavion 5y agoDo you have examples? I'm not aware of any libraries using async for things that aren't IO-related, at least not the important / popular ones.
- Animats 5y agoWhat I'm doing looks like this.[1] That's frantically loading and unloading textures at different resolutions as the camera moves to stay within GPU memory limits, but you don't see that happening. (It's running at about 60FPS, but the video capture program, Kazaam, doesn't capture in sync with the graphics card, which is why there's some tearing in the videos. Switching to OBS for capture.) A rendering library I use added an 'async' framework, but it turns out using it is not going to be mandatory. Much relieved. [1] https://vimeo.com/user28693218 https://vimeo.com/user28693218
- Arnavion 5y agoI'm asking for examples of Rust crates that have added async on functions that are not IO-bound, not examples of your work.
- moonchrome 5y ago>"Async" is optimized for I/O bound programs. Also works better than callbacks for single threaded/event loop scenarios like JS or some GUI framework (with the caveat that you need to make sure you are resuming on the event loop if environment is multithreaded).
- mpweiher 5y agoHmm...isn't that event loop I/O and such programs I/O bound?
- moonchrome 5y agoNot necessarily, if you want to do non-blocking compute on client you would ideally spin off a worker thread but if not available you can periodically yield from your compute task to the event loop to avoid blocking.
- jacquesm 5y agoAsync is pushing Rust where it shouldn't really be going anyway: to be a Node.js replacement. The actor model is far better suited to solve these kind of problems to begin with.
- Animats 5y agoI get that feeling, too. My general comment is that web back-end stuff should be written in Go. Go is designed for exactly that. Goroutines are lightweight but preemptable, so you don't have the async vs. thread problem at all. There's only one way to do it. Most of the Go libraries you need for web back-end are well-exercised, because they were written at Google and are used internally. Writing in Rust takes more engineering effort and is less agile. You can code yourself into a corner which requires considerable rewriting, or unsafe code, to escape. What I'm writing in Rust is a metaverse client. These are not common yet. They have both the real-time problems of a game client and the unexpected content-driven loads of a web browser. The goal is to keep the frame rate constant as a stream of update transactions comes in, perhaps hundreds per second. The transactions modify the scene graph and change what's on screen. So refresh is in one high-priority thread, while updating is in other low priority threads. Overload bursts happen routinely and have to be dealt with, updating near and large things first. In MMOs, such overloads are usually detected and prevented during game design, but a metaverse viewer does not have that luxury. Existing clients tend to choke and stall on overload, but with enough CPUs working on the problem, that can be overcome. It's a fun problem. It doesn't have the shape of most common applications.
- jacquesm 5y agoYes, I can see that as a good match for Rust. When you started to describe it I thought 'Erlang' but as the description progressed it clearly is a bad match for Erlang due to the performance requirements. But typical web stuff: Go or even Java would seem to be a much better choice than Rust, for the same reasons I would not write a typical web service in C or C++.
- newlisp 5y agoThe actor model is far better suited to solve these kind of problems to begin with. Depends on whether you are talking about a single machine or a distributed program.
- knuthsat 5y agoasync, when looked at from the "writing code" perspective is horrible. it pollutes every function with async annotation. it allows you to quickly write the code imperatively by just annotating a function and using the async function inside it. it results in code that is massively polluted with async when at most places you never had to use it at all (if you just restructured the code). having async be as verbose as possible would enforce people to not use it as much and to structure their code to have async bits nicely separated from non-async bits.
- sitkack 5y agoThat would be a kind of a sintax, no?
- knuthsat 5y agoI guess it depends on what OP is talking about. Async code can be written with callbacks (callback hell incoming), or with Futures/Promises, or with syntax sugar like async (do notation). Whatever the reason is for using async, I believe that the mismatch happens not because Async is optimized for I/O bound programs but because it's insanely easy to use (in Rust, Javascript).