Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby only creating 3 threads at a time

I am trying to run 500 clients that send some request to the server simultaneously for load testing purpose. The client is a ruby program again. Sounds trivial. But I am facing weird problem with ruby threads. My code looks like this -

n = 10

n.times do
  Thread.new do
    `calc`
  end
end

The code is a sample. I'm just trying to run calc command from command line (If you are trying this code on platform other than windows please replace it with some command that works on your command line or shell). This will later be replaced by 'ruby my_client.rb' and also the value of n will be set to 500 (or whatever).

The problem that I am facing here is that regardless the number of threads I want to create, only 3 threads are created at a time. That is only 3 calc windows are opened simultaneously. The remaining threads just wait in a queue waiting for termination of these 3 threads. May be it has something to do with blocking and non-blocking calls. But I tried Java-equivalent of the same program and it worked perfectly. Its an old saying that threads in ruby are not recommended. Is it true that this is a problem with Ruby's threads or am I doing something wrong?

like image 371
Chirantan Avatar asked Feb 14 '09 04:02

Chirantan


People also ask

How many threads can be executed at a time Ruby?

The Ruby interpreter handles the management of the threads and only one or two native thread are created.

Is Ruby multi-threaded?

Ruby makes it easy to write multi-threaded programs with the Thread class. Ruby threads are a lightweight and efficient way to achieve concurrency in your code.

Can Ruby use multiple cores?

Threading. If you're going to want multi-core threading, you need to use an interpreter that actively uses multiple cores. MRI Ruby as of 2.1. 3 is still only single-core; JRuby and Rubinius allow access to multiple cores.

Is Ruby single threaded or multi-threaded?

The Ruby Interpreter is single threaded, which is to say that several of its methods are not thread safe. In the Rails world, this single-thread has mostly been pushed to the server.


1 Answers

The problem you're observing is specific for GUI applications. It gets much better when you run command-line stuff inside workers.

With example below I can run 200 wget instances just fine, which is probably enough for your load testing goals.

n = 200

threads = []
(1..n).each do |i|
  threads << Thread.new do
    puts `wget google.com` # forgive me google
    sleep 10
    puts "#{i} done"
  end
end

threads.each do |t| # wait until all workers are done
  t.join
end

You could probably get many more workers if you switch from wget to Ruby code for fetching web pages. And still, you should remember that Ruby threads scales only that far. Don't expect many thousands or parallel threads to work fine -- try subprocesses or continuation-based approach instead.

like image 158
Alexander Lebedev Avatar answered Oct 16 '22 11:10

Alexander Lebedev