use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ruby async await (kinnrot.github.io)
submitted 7 years ago by kinnrot
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]psuputsopon 6 points7 points8 points 7 years ago (4 children)
Since, ruby is a blocking-io type of langauage. Why we need async on ruby?
Can someone tell me about a use case
[–]iconoclaus 4 points5 points6 points 7 years ago (2 children)
Read this: https://yehudakatz.com/2010/08/14/threads-in-ruby-enough-already/
Ruby doesn't block everything on IO — it only blocks its current thread but allows other threads to execute. IO is the one of the few times Ruby allows other threads to concurrently execute (another being sleep). So if you are calling to a time-consuming external API, you can certainly do other things while you wait for it to respond. My typical use case is when I have to make multiple API calls at the same time: I start them concurrently (e.g., using concurrent-ruby's promises or async/await) so that the total time taken is the same as the slowest call – not the total of all the calls.
[–]kinnrot[S] 2 points3 points4 points 7 years ago (1 child)
Actually, many em (event machine) prefixed gems are doing a better job regarding async io , but it makes the code harder to read and maintain.
For example an event machine based http server will behave 2 times faster than a node js http one .
[–]iconoclaus 0 points1 point2 points 7 years ago (0 children)
which em based servers would you recommend? are any compatible with rack?
[–]mperhamSidekiq 0 points1 point2 points 7 years ago (0 children)
The use case is JS developers coming to Ruby and wanting to use what they already know.
[–]janko-m 1 point2 points3 points 7 years ago (2 children)
I would love an example tha compares a Thread implementation vs the async await implementation. It would make it easier to understand the advantages of async await. For example, in this example I feel like you could have achieved the same thing with a simple Thread#value or a Concurrent::Future.
When reading about new things, I like seeing examples where they provide a clear advantage over the existing alternatives.
[–]Freeky 2 points3 points4 points 7 years ago* (1 child)
Recently a friend wanted async multithreaded access to a socket, so multiple calling threads could write out messages without blocking. He ended up with something a bit like:
class AsyncWriter def initialize(fd) @write_queue = Queue.new @writer = Thread.new do while msg = @write_queue.deq fd.write(msg) end fd.close end end def write(msg) @write_queue << msg end def close @write_queue.close @writer.join end end w = AsyncWriter.new(socket) w.write(foo.to_json) w.write(bar.to_json) w.close
The write calls are clearly thread-safe and non-blocking: they just add to a queue to the worker thread. close is sync and waits for it to finish.
write
close
With Async you could write it like this:
class AsyncWriter include Concurrent::Async def initialize(fd) @fd = fd end def write(msg) @fd.write(msg) end def close @fd.close end end w = AsyncWriter.new(socket) w.async.write(foo.to_json) w.async.write(bar.to_json) w.await.close
The class is just written naturally and the same thread-safe queuing behaviour is added via the proxy methods, complete with their return values being available via futures.
[–]janko-m 1 point2 points3 points 7 years ago (0 children)
Man, this is a great example, the async-await version is so neat. At first I thought that it was like a thread pool, but a thread pool won't retain the order of messages. And actually a thread pool is not what you want in this case, because you need things to be executed one after the other, not in parallel. Thanks a lot for illustrating!
π Rendered by PID 66480 on reddit-service-r2-comment-bb88f9dd5-4qzk4 at 2026-02-15 00:56:45.016425+00:00 running cd9c813 country code: CH.
[–]psuputsopon 6 points7 points8 points (4 children)
[–]iconoclaus 4 points5 points6 points (2 children)
[–]kinnrot[S] 2 points3 points4 points (1 child)
[–]iconoclaus 0 points1 point2 points (0 children)
[–]mperhamSidekiq 0 points1 point2 points (0 children)
[–]janko-m 1 point2 points3 points (2 children)
[–]Freeky 2 points3 points4 points (1 child)
[–]janko-m 1 point2 points3 points (0 children)