News for hackers. Stuff that matters.
Re:Building a shared vision for Async Rust by lmm on Thursday March 18, 2021 @10:49PM (#26509778)
You want libraries to be able to offer implementations that are polymorphically async-or-not (perhaps using higher-kinded types). That's how I'm used to working in Scala.
Re:Building a shared vision for Async Rust by geofft on Thursday March 18, 2021 @11:20PM (#26509894)
This is also a problem over in Python-land. One of the interesting approaches is automatically generating non-async code from async code by editing out the async annotations (at the source level). The Trio folks call this "bleaching" the code, in reference to the "What color is your function?" blog post https://journal.stuffwithstuff.com/2015/02/01/what-color-is-... complaining about how the common approach to async in JavaScript divides the world into "red" (async) and "blue" (sync) functions that interoperate poorly - you can bleach your code if it's the wrong color.

https://github.com/urllib3/urllib3/issues/1323 is a discussion of this. "Solution: we maintain one copy of the code – the version with async/await annotations – and then a little script maintains the synchronous copy by automatically stripping them out again. It's not beautiful, but as far as I can tell all the alternatives are worse.

https://github.com/python-trio/unasync is the current production implementation of this approach.

Re:Building a shared vision for Async Rust by kmeisthax on Friday March 19, 2021 @12:10AM (#26510091)
In Rust, you don't need to do this. If you want a blocking version of a non-blocking function, you spin on poll. Several async libraries ship a block_on function that does just that. If you can't do that, but don't want to bring in a proper big-boy executor; you can spin off another thread, have it block, and join it when it's done. (or use something like crossbeam's scoped threads)

Colored functions are a problem in JavaScript because it has to share an event loop with the rest of the browser. For Node.js and Python, that is less of an issue, but those languages are also either practically or actually single-threaded, which also means async behavior is contagious. Rust does not have this problem, at least not until you're writing WASM programs with it (in which case, you're restricted to sharing one thread with the rest of the browser again).

Re:Building a shared vision for Async Rust by geofft on Friday March 19, 2021 @12:20AM (#26510127)
Well, 'Animats expresses a desire in another comment for functions not to go off and spawn a new thread.

I do somewhat agree with that desire - I don't think that this is a problem for performance, but since most of my use of Rust is dropping it into existing C code, I think that spawning a thread is likely to have annoying visible side effects on the calling code, which might not be expecting me to do that.

Using a proper, single-threaded executor and running it on the current thread seems like it would work, yes. (To be fair, I also feel like just having the sync version of a Python API call "trio.run(self.equivalent_async_api)" would probably also work, and I don't totally follow why that's insufficient....)

Re:Building a shared vision for Async Rust by h0l0gr4ph1c on Friday March 19, 2021 @02:41AM (#26510620)
> "Using a proper, single-threaded executor and running it on the current thread seems like it would work, yes. (To be fair, I also feel like just having the sync version of a Python API call "trio.run(self.equivalent_async_api)" would probably also work, and I don't totally follow why that's insufficient....)"

Why do I need all this other bloat just to run a function? Why can't I just run the function?

Re:Building a shared vision for Async Rust by geofft on Friday March 19, 2021 @02:50AM (#26510667)
I'm not sure exactly what you're asking, but I think it's either answered by the "What color is your function?" article I linked above, or by the answer that this is exactly why unasync exists and why I suggested that approach is worth considering, or by the answer that you can, in fact, just run the function and the "bloat" (which is just syntactic bloat - note that performance is generally going to be better!) is taken care of behind the scenes by a wrapper that calls an executor for you.
Re:Building a shared vision for Async Rust by h0l0gr4ph1c on Friday March 19, 2021 @03:21AM (#26510780)
yes sorry, those were rhetorical questions. Your point about asking you fail to see why not using a blocking executor to deal with the async code. My problem is with needing the executor at all. I must have skipped a couple of you previous pints in this thread. Apologies about that...

Maybe we should start trying to think about async as being something can use if they want and ignore if they want. Code being async compatible rather than async required.

Re:Building a shared vision for Async Rust by geofft on Friday March 19, 2021 @10:00AM (#26513399)
How would this work? (I do really think this is the right model, I'm just trying to figure out what that model is, exactly. :) )

Let's say I have code like this, in Python asyncio:

    class ShardedDBClient:
        async def query(self, key):
            tasks = [self.query_shard(key) for shard in self.shards]
            results = await asyncio.gather(*tasks)
            for partial_result in results:
                if key in partial_result:
                    return partial_result[key]
            return None
How do you run this without an executor?

The obvious way to make it not be "async required" is to say, we get rid of the async/await keywords - but what do you do with that "await asyncio.gather" instruction? Do you call each of those callbacks serially?

Generally, even in Rust (perhaps especially in Rust), I would expect this to use some OS facility for waiting on multiple sockets (possibly even just boring select(), but preferably epoll/kqueue) to send a bunch of database requests out in parallel and then wait on all their sockets to handle responses as they arrive. I would expect that even if my own code doesn't involve async/await at all.

The easy way to implement that is

        def sync_query(self, key):
            return asyncio.run(self.query(key))
which creates an asyncio executor just to run that one function.

This is going to be a lot faster than querying those shards one at a time! And it also can semantically change how the library behaves - imagine that there's a timeout parameter, and I set a 100ms timeout. I probably mean that to be 100ms for the entire operation, not 100ms per request, but I probably also don't expect my calls to always fail if each query takes 10ms and there are more than 10 shards.

The downside is that this library is quietly using asyncio without you knowing. But how exactly is that a downside? I already expect the library to be using select/epoll/kqueue without me knowing. And in a language like Rust, the executor should basically compile out - it should be a "zero-cost abstraction" compared to writing the event-handling code by hand.

Re:Building a shared vision for Async Rust by pas on Friday March 19, 2021 @04:56AM (#26511269)
As long as the async code doesn't depend on calling itself concurrently it should be straightforward to simply execute it on the current thread, right? (Basically using an executor that has 1 thread, the current thread.)

And it'd be great to check and optimize away all this at compile time.

Re:Building a shared vision for Async Rust by lmm on Friday March 19, 2021 @12:25AM (#26510147)
It's a problem if your function to query a database or whatever instantiates a bunch of futures and yields between them when you're just going to block. Given that Rust is designed for environments that are so limited that they can't afford garbage collection, that stuff is proportionately pretty costly.
Re:Building a shared vision for Async Rust by millstone on Friday March 19, 2021 @01:20AM (#26510356)
Does spin-on-poll imply actual spinning (100% CPU) or actual blocking (waiting in a syscall)?
Re:Building a shared vision for Async Rust by dodobirdlord on Friday March 19, 2021 @01:34AM (#26510403)
That would have to be determined at the discretion of the implementer of the library. Rust is after all capable of being used to write code that will run in a context where there is no kernel and thus no syscalls.
Re:Building a shared vision for Async Rust by millstone on Friday March 19, 2021 @02:51AM (#26510669)
I don't get it - what library? I may have an `async` function and this is part of the Rust language, not tied to any library. Can I make it into a blocking function without 100% CPU?
Re:Building a shared vision for Async Rust by pas on Friday March 19, 2021 @04:52AM (#26511246)
"async/await" is just syntactic sugar for a function that returns a Future plus a state machine at yield points. You need a library (executor) to run that Future (execute that function).

The Rust standard library's block_on uses a global ThreadPool, and the docs recommend using a LocalPool if you need finer grained control.

So, to answer your question it depends on the executor (the thing that implements block_on).

Re:Building a shared vision for Async Rust by steveklabnik on Friday March 19, 2021 @09:41AM (#26513189)
(To be clear, block_on is not in the Rust standard library.)
Re:Building a shared vision for Async Rust by pas on Friday March 19, 2021 @10:01AM (#26513407)
ah, darn, thanks!

So for anyone reading for the correct details: the block_on I was thinking of is part of the futures crate, which is not in std, it's not an "official library".

Re:Building a shared vision for Async Rust by steveklabnik on Friday March 19, 2021 @10:59AM (#26514088)
It's all good! It was proposed, but decided to have an RFC first. Your point is still correct, just wanted to make sure to clarify this detail :)
Re:Building a shared vision for Async Rust by lmm on Friday March 19, 2021 @12:20AM (#26510123)
Wow. Reminds me of those go projects that use templated code with Canadian Aboriginal Syllabics to work around the lack of generics.