Doing non blocking async IO with Ruby

Hi,
I’m coming to ruby from other programming languages like C# and Swift
where you can handle web async calls with built in feature like
async/await, so you can fetch db result, wait them to perform an
operation once the result are returned, but still be able to do other
things while waiting for the results.

On Ruby, if I want to query a database and not block the system, without
necessarily creating a new thread, is it possible ? What are the common
ways in this language ?

thanks

That totally depends on implementations of database drivers. Generally
you can use nonblocking IO (see documentation of class IO).

I guess the usual solution is to use threads for that since most libs
for DB access etc. implement blocking calls.

Benjamin M. wrote in post #1169971:

On Ruby, if I want to query a database and not block the system, without
necessarily creating a new thread, is it possible ? What are the common

https://www.igvita.com/2008/09/05/asynchronous-database-access-in-ruby/

but in general how do people deal with this ?I come from a native app
iOS background, where I run blocking tasks in the background, that is in
another thread so the user is never stuck.

On a server level, a single user can block all other users if they try
to do concurrent tasks. So for example, if a user upload a file and that
takes 5 seconds, or run a long query, during those seconds, the server
can’t serve any other users. Is that correct ? Or am I missing something
?

I never did server side programming yet, and this seems to be a huge
issue, yet I don’t find much documentation about it.
How do people deal with concurrency in ruby usually, they just create
other threads ? I visited many site running ruby, or pythong, and they
are not particularly slow, yet I can’t understand how, considering the
fact that those systems can’t run concurrent tasks.

Please advise.

I come from a native app
iOS background, where I run blocking tasks in the background,
that is in another thread so the user is never stuck.<

The principles are the same everywhere. In your previous programming
language, you say that threads are created to run tasks that don’t block
the main process, but for ruby your requirement is that no additional
threads/processes can be used?

I visited many site running ruby, or pythong, and they
are not particularly slow, yet I can’t understand how, considering the
fact that those systems can’t run concurrent tasks.

  1. Both ruby and python can run concurrent tasks.
  2. The websites you visited are probably not using a ruby/python server
    anyway. You don’t need a server written in ruby/python to execute
    ruby/python programs. For instance, Apache can run ruby/python
    programs.

On Ruby, if I want to query a database and not block the system,
without necessarily creating a new thread,<

You either have to create a new thread or a new process. See the
following:

  1. IO#popen
  2. Open3 module
  3. Socket module
  4. EventMachine

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs