5 ms·
> any blocking operation (a method where Ruby interpreter waits) is compatible with Async and will work asynchronously within Async code block with Ruby 3.0 and
by sizediterable 5y ago
> any blocking operation (a method where Ruby interpreter waits) is compatible with Async and will work asynchronously within Async code block with Ruby 3.0 and later.
That's pretty magical. Does this mean it would be possible to implement structured concurrency [0], but without the function coloring problem?
Regardless, I think I prefer how this looks from a code read/writeability perspective compared to Zig's or Swift's (and potentially future Rust's [2]) approaches.
[0] https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful https://vorpus.org/blog/notes-on-structured-concurrency-or-g...
[1] https://kristoff.it/blog/zig-colorblind-async-await/ https://kristoff.it/blog/zig-colorblind-async-await/
[2] https://blog.yoshuawuyts.com/async-overloading/ https://blog.yoshuawuyts.com/async-overloading/
- oezi 5y agoI am actually not a 100% how far the compatibility shows. When I test Async gem with httparty and open-uri, there is no speed-up when comparing with threads: https://gist.github.com/coezbek/07061fb7de2b036bf5c69f8a8b8cc3e5 https://gist.github.com/coezbek/07061fb7de2b036bf5c69f8a8b8c... I might be using it wrong...
- oezi 5y agoHa! This only works with Ruby 3.0, because only since then are standard library function using async-compatible methods internally.
- brunosutic 5y agoAsync Ruby is colorless! It's obvious from the examples provided in the post. For example, you can use the method `URI.open` both synchronously and asynchronously. It's something I didn't want to mention in the article because it's a relatively advanced async concept, but yea - Async Ruby is already colorless and it's great.
- gpderetta 5y agoIIRC ruby has first class continuations, so it doesn't have to deal with any async/await nonsense.
- brunosutic 5y agoThat's right - Async Ruby uses fibers (stackful coroutines) as a concurrency primitive. There's a lot to say about this, but the end result is that we get to write simple synchronous code and it can run asynchronously.
- g8oz 5y agoN00b question: would the fibers being introduced in PHP 8.1 enable the same functionality in that language? https://php.watch/versions/8.1/fibers https://php.watch/versions/8.1/fibers
- nixpulvis 5y agoOn a tangential note. > Async Ruby is colorless! I find it deeply irritating that this is how we choose to describe things these days. Rather than talking about modality, or even using a few more words to describe the actual differences in terms of the underlying programing language models. Telling me that Ruby's async is colorless implies that I know about the description of programming models as "colors" of code, which was just some random analogy someone came up with which caught on. Nothing about this has anything to do with color. Even syntax highlighting doesn't really fit. This is akin to particle physicists getting cheeky and calling one of the quantum states "color" when it has nothing to do with wavelength (correct me if I'm wrong). This is an appeal to stop trying to be so cute. Thank you.
- brunosutic 5y agoRed-black tree, a well-known algorithm also uses color references https://en.wikipedia.org/wiki/Red–black_tree https://en.wikipedia.org/wiki/Red–black_tree
- bradgessler 5y agoWhat is “colorless” suppose to mean? This is the first time I’ve seen it used.
- vmarquet 5y agoI think it's a reference to this blog post https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...
- auno 5y agoWhich in turn is making a reference to an even older post at https://ansuz.sooke.bc.ca/entry/23 https://ansuz.sooke.bc.ca/entry/23.
- jsnell 5y agoContext: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ https://journal.stuffwithstuff.com/2015/02/01/what-color-is-...
- DangitBobby 5y agoAs someone who operates mainly in Python, I am so jealous. As far as I'm aware [1], you have to re-write your tooling to take advantage of async in Python. Does anyone have any insight into why Python async doesn't work the same way? Does it come down to fundamental language differences? 1. https://stackoverflow.com/a/63179518/4728007 https://stackoverflow.com/a/63179518/4728007
- pansa2 5y ago> Does anyone have any insight into why Python async doesn't work the same way? Ruby async seems to be implemented using stackful coroutines. IIRC Guido has been opposed to adding these to core Python, preferring stack-less coroutines because they require every yield-point to be explicit (i.e. marked with `await`). There are libraries for a python that support stackful coroutines, such as gevent.
- brunosutic 5y ago> Does it come down to fundamental language differences? No, I don't think there's anything fundamentally different. Ruby 3.0 implements a "fiber scheduler" feature that enables "colorless Async". Fiber scheduler is an obscure Ruby feature, but the end result is brilliant. It was also a huge amount of work. Side note: Fiber scheduler was implemented by the same guy who created Async Ruby - Samuel Williams. This guy is the mastermind (and master-coder) behind this project.
- sizediterable 5y agoI'm optimistic that this will be less painful as more of the Python ecosystem becomes async-friendly. We have `aiohttp` as a suitable replacement for `requests`, and major libraries like Django (not ORM yet), Flask, FastAPI, SQLAlchemy now have async support as well.
- DangitBobby 5y agoIt has already gotten less painful as time goes on. However, in my experience so far if I need e.g. Django to use Google Cloud Storage for media storage, I need to write my own shims that marry together an async alternative to Google's Python SDK (which I have no idea if there are any plans for async support) with third part django packages that I'd normally use. The experience isn't terrible, but I end up spending a lot of time trying to making consistent calling conventions. And I think I've come to the conclusion that any re-usable code I write for IO should just be written async with magic sync wrappers for use in sync contexts.
- azth 5y agoThe first reference is the same route that Java is taking with its green thread implementation by means of Project Loom[0]. [0] https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part2.html#structured-concurrency https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part2....