# Problem with multithreading on Ruby On Rail

**URL:** <https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795>\
**Category:** Rails\
**Created:** [September 30, 2010, 10:06am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795 "2010-09-30T10:06:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://www.ruby-forum.com/u/yann)\
**Post date:** [September 30, 2010, 10:06am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795/1 "2010-09-30T10:06:09Z")

</div>

Hi,

first of all, sorry for my english.

to start, here my config :  
Server linux : ubuntu 8.4  
Rails : 2.3.4  
Ruby : 1.8.7 (2010-06-23 patchlevel 299) [x86\_64-linux]

I wanted to test the charge capacity of my server, and I saw a problem  
with the multithrearding on Ror.

## Here the config file on the server config/environments/development.rb Code :

## config.threadsafe!

Here the ruby file on the server  
app/controllers/hello\_controller.rb  
class HelloController \< ApplicationController

```
    def word
            $startTime = Time.now
            for valeur in 1..10000000
            end
            $stopTime = (Time.now - $startTime)
            render :text => "Hello Word!!!!, id: "+params["id"]+",

```

temps: "+$stopTime.to\_s  
end  
end

And the script of “loading test” :  
helloWord.sh  
#!/bin/bash  
count=0  
while [$count -lt $1]  
do  
php helloWord.php $count &  
count=`expr $count + 1`  
done

And the client script  
$url = “http://”.URL\_AMV.“/hello/word?id=”.$id;  
$timeStart = microtime(TRUE);  
echo “end of “.$id.” to “.$timeStart.”\n”;  
$retour = file($url);  
$timeStop = microtime(TRUE);  
echo “end of “.$id.” to “.$timeStop.”\n”;  
echo “Total for “.$id.” =\> “.$totalTime.”\n”;  
echo $url.“\n”;  
print\_r($retour);

The result is :  
./helloWord.sh 5

start of 0 to 1285767141.33

start of 1 to 1285767141.332

start of 2 to 1285767141.37

start of 3 to 1285767141.3795

start of 4 to 1285767141.406  
end of 0 to 1285767142.2877

Total for 0 =\> 0.958  
[http://87.98.167.204:4445/hello/word?id=0](http://87.98.167.204:4445/hello/word?id=0)  
Array  
(  
[0] =\> Hello Word!!!, id: 0, temps: 0.760337  
)  
end of 1 to 1285767143.1712

Total for 1 =\> 1.839  
[http://87.98.167.204:4445/hello/word?id=1](http://87.98.167.204:4445/hello/word?id=1)  
Array  
(  
[0] =\> Hello Word!!!, id: 1, temps: 0.760326  
)  
end of 2 to 1285767143.9855

Total for 2 =\> 2.616  
[http://87.98.167.204:4445/hello/word?id=2](http://87.98.167.204:4445/hello/word?id=2)  
Array  
(  
[0] =\> Hello Word!!!, id: 2, temps: 0.760426  
)  
end of 3 to 1285767144.8673

Total for 3 =\> 3.488  
[http://87.98.167.204:4445/hello/word?id=3](http://87.98.167.204:4445/hello/word?id=3)  
Array  
(  
[0] =\> Hello Word!!!, id: 3, temps: 0.760231  
)  
end of 4 to 1285767145.6874

Total for 4 =\> 4.281  
[http://87.98.167.204:4445/hello/word?id=4](http://87.98.167.204:4445/hello/word?id=4)  
Array  
(  
[0] =\> Hello Word!!!, id: 4, temps: 0.764974  
)

and the log is :  
Processing HelloController#word (for 80.13.147.95 at 2010-09-29  
15:31:53) [GET]  
Parameters: {“id”=\>“0”}  
Completed in 763ms (View: 0, DB: 1) | 200 OK [[http://87.98.167.204/](http://87.98.167.204/)  
hello/word?id=0]  
SQL (0.2ms) SET client\_min\_messages TO ‘panic’  
SQL (0.1ms) SET client\_min\_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29  
15:31:54) [GET]  
Parameters: {“id”=\>“1”}  
Completed in 763ms (View: 0, DB: 1) | 200 OK [[http://87.98.167.204/](http://87.98.167.204/)  
hello/word?id=1]  
SQL (0.2ms) SET client\_min\_messages TO ‘panic’  
SQL (0.1ms) SET client\_min\_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29  
15:31:55) [GET]  
Parameters: {“id”=\>“2”}  
Completed in 763ms (View: 0, DB: 1) | 200 OK [[http://87.98.167.204/](http://87.98.167.204/)  
hello/word?id=2]  
SQL (0.2ms) SET client\_min\_messages TO ‘panic’  
SQL (0.1ms) SET client\_min\_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29  
15:31:56) [GET]  
Parameters: {“id”=\>“3”}  
Completed in 763ms (View: 0, DB: 1) | 200 OK [[http://87.98.167.204/](http://87.98.167.204/)  
hello/word?id=3]  
SQL (0.2ms) SET client\_min\_messages TO ‘panic’  
SQL (0.1ms) SET client\_min\_messages TO ‘notice’

Processing HelloController#word (for 80.13.147.95 at 2010-09-29  
15:31:57) [GET]  
Parameters: {“id”=\>“4”}  
Completed in 767ms (View: 0, DB: 1) | 200 OK [[http://87.98.167.204/](http://87.98.167.204/)  
hello/word?id=4]

what are we seeing?

- 5 process run together
- Ruby take the same time for each process (760 ms)
- BUT : RoR wait the end of each process before answer to client.

finally :  
I think, there is a probleme somewhere. but WHERE?? could you help me  
please?

regards,  
Yann (geek)

---

<div class="post-metadata">

**Author:** ![yann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://www.ruby-forum.com/u/yann)\
**Post date:** [September 30, 2010, 10:17am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795/2 "2010-09-30T10:17:56Z")

</div>

On Sep 30, 9:06 am, Yann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what are we seeing?
> 
> - 5 process run together
> - Ruby take the same time for each process (760 ms)
> - BUT : RoR wait the end of each process before answer to client.
> 
> finally :  
> I think, there is a probleme somewhere. but WHERE?? could you help me  
> please?

Rails runs single-threadedly by default. config.threadsafe! (in one of  
your environment configuration file) will enable more than one request  
to be processed at a time although limitations of MRI’s threadeding  
mean you might not see the difference you expect (and i think earlier  
versions of mongrel only even dispatch one request to rails at a time,  
because rails used to be non threadsafe)

Fred

---

<div class="post-metadata">

**Author:** ![yann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://www.ruby-forum.com/u/yann)\
**Post date:** [September 30, 2010, 10:44am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795/3 "2010-09-30T10:44:10Z")

</div>

Frederick C. wrote:

> On Sep 30, 9:06ï¿½am, Yann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > what are we seeing?
> > 
> > - 5 process run together
> > - Ruby take the same time for each process (760 ms)
> > - BUT : RoR wait the end of each process before answer to client.
> > 
> > finally :  
> > I think, there is a probleme somewhere. but WHERE?? could you help me  
> > please?
> 
> Rails runs single-threadedly by default. config.threadsafe! (in one of  
> your environment configuration file) will enable more than one request  
> to be processed at a time although limitations of MRI’s threadeding  
> mean you might not see the difference you expect (and i think earlier  
> versions of mongrel only even dispatch one request to rails at a time,  
> because rails used to be non threadsafe)
> 
> Fred

thank you.

I comment this line :  
config.cache\_classes = false

In the config, And I see it work better.  
But, could you answer to this question :

When only one process run, the client wait 0.907 sec.  
When 2 process run together, each client wait 1.705 sec.  
When 5 process run together, each client wait 4.073 sec.

Whereas ruby take 0.760 sec each time, for all process in all  
configuration.

Why?

---

<div class="post-metadata">

**Author:** ![yann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://www.ruby-forum.com/u/yann)\
**Post date:** [September 30, 2010, 11:47am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795/4 "2010-09-30T11:47:16Z")

</div>

Frederick C. wrote:

> On Sep 30, 9:44ï¿½am, Yann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I comment this line :  
> > config.cache\_classes = false
> > 
> > In the config, And I see it work better.
> 
> You definitely want that - class reloading is non threadsafe.
> 
> > But, could you answer to this question :
> > 
> > When only one process run, the client wait ï¿½0.907 sec.  
> > When 2 process run together, each client wait 1.705 sec.  
> > When 5 process run together, each client wait 4.073 sec.
> 
> I checked mongrel’s source code - it’s still got that mutex inside the  
> rails dispatcher ( a relic really from when that was still needed) so  
> it will never dispatch more than one request to rails at a time.
> 
> Fred

I was told, start server with this commande :  
server/script : is BAD!!

to perform my serveur, its better to use : unicorn/passenger/thin or  
mongrel.

I let’s go to try this…

What to you think about it?

---

<div class="post-metadata">

**Author:** ![yann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://www.ruby-forum.com/u/yann)\
**Post date:** [September 30, 2010, 11:35am UTC](https://www.ruby-forum.com/t/problem-with-multithreading-on-ruby-on-rail/195795/5 "2010-09-30T11:35:47Z")

</div>

On Sep 30, 9:44 am, Yann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I comment this line :  
> config.cache\_classes = false
> 
> In the config, And I see it work better.

You definitely want that - class reloading is non threadsafe.

> But, could you answer to this question :
> 
> When only one process run, the client wait 0.907 sec.  
> When 2 process run together, each client wait 1.705 sec.  
> When 5 process run together, each client wait 4.073 sec.

I checked mongrel’s source code - it’s still got that mutex inside the  
rails dispatcher ( a relic really from when that was still needed) so  
it will never dispatch more than one request to rails at a time.

Fred
