# HELP! Problems with multithreading and Controller

**URL:** <https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912>\
**Category:** Rails\
**Created:** [August 30, 2006, 8:23am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912 "2006-08-30T08:23:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [August 30, 2006, 8:23am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/1 "2006-08-30T08:23:35Z")

</div>

Hi all,

I am a newbie to Ruby-Rails. I am basically from Java environment. I am  
implementing a DB Schema Analyzer in Ruby-Rails.

I have a rhtml which submits request to analyze query

## I have a ThreadedSchemaParser class in lib folder which is sub-class of Thread. Each request for analysis must execute on a separate thread. My controller have the following code:

def tparse  
@schemaParser = ThreadedSchemaParser.new  
@schemaParser.set\_schema\_properties(params[:query])  
session[:result] = @schemaParser.run  
redirect\_to :action =\> “result\_view”  
end

# def result\_view if session[:result] @flash[session[:result][‘flag’]] = session[:result][‘msg’] @filelist = session[:result][‘file\_list’] @user = Logins.find(@activity.login\_id).name end end

Once the parse is over the result is viewd in result\_view.rhtml

## I have my ThreadedSchemaParser.rb class code like:

class ThreadedSchemaParser \< Thread

def run  
return query\_parse  
end

def query\_parse

/\* some code goes here (around 60 - 80 sec per req)\*/

end

def set\_schema\_properties(query)  
@query = query  
end

# end

Each analysis takes, on an average, 60 - 90 sec. If i am opening  
multiple client browsers and submitting analyze request the same time  
then every user can only see their result after all the current requests  
have been parsed.

For example:

User1 enters a query and submits the request - 0 sec  
Processing started for User1 - 1 sec  
User2 enters a query and submits the request - 10 sec  
Processing finished for User1 - 95 sec  
Processing started for User2 - 96 sec  
Processing finished for User2 - 176 sec  
Rendering result\_view for User1 - 177 sec  
Rendering result\_view for User2 - 177 sec

The response time for User1 is 176 sec and User2 is 167 sec. My  
questions are:

1. Why does all result\_view get rendered the same time irrespective of  
the requests? How can i resolve this?

2. Will migrating code from the controller to the view do any help?

3. How can i reduce the response time (Either by using threads or any  
other method)?

---

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [August 30, 2006, 9:58am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/2 "2006-08-30T09:58:18Z")

</div>

\> \> 1. Why does all result\_view get rendered the same time irrespective of \> the requests? How can i resolve this? \> \> 2. Will migrating code from the controller to the view do any help? \> \> 3. How can i reduce the response time (Either by using threads or any \> other method)? \>

I think the reason why your threads don’t behave like threads is that  
you have overwritten the run() method.

You shouldn’t subclass Thread. Instead, use blocks like this:

t = Thread.new( some\_variable ) do |some\_variable|

# do stuff with the variable here

Thread.current[“result”] = some\_result\_calculated  
end

Once the thread finished running, you can use t[“result”] to get the  
value some\_result\_calculated.

If you need to do any more complex calculations in the thread,  
refactor those into a separate class and do something like

t = Thread.new( query ) do |query |  
Thread.current[“result”] = QueryProcessor.new.query\_parse(query )  
end

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [August 30, 2006, 10:24am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/3 "2006-08-30T10:24:23Z")

</div>

On Aug 30, 2006, at 12:46 AM, Max M. wrote:

> > other method)?  
> > end
> 
> Cheers,  
> Max

```
Also you will only cause yourself problems spinning off threads in

```

your rails app like that. Especially if they take 90 seconds to  
complete. The reason you see the weird timing issues is because when  
you access rails from a broswer the requests get serialized behind a  
mutex so they can only run one at a time. You need to get another  
process running and communicate with that adn have your threads run  
in the secondary proces away from the request/response cycle. I wrote  
a plugin for this you should check out here[1]

-Ezra

[1] [http://backgroundrb.rubyforge.org](http://backgroundrb.rubyforge.org)

---

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [September 1, 2006, 7:22am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/4 "2006-09-01T07:22:52Z")

</div>

Hi Ezra,

> Also you will only cause yourself problems spinning off threads in  
> your rails app like that. Especially if they take 90 seconds to  
> complete. The reason you see the weird timing issues is because when  
> you access rails from a broswer the requests get serialized behind a  
> mutex so they can only run one at a time. You need to get another  
> process running and communicate with that adn have your threads run  
> in the secondary proces away from the request/response cycle. I wrote  
> a plugin for this you should check out here[1]

Thanks for the reply. I agree with you that spinning off threads in  
rails create a lot of unpleasant behavior. I believe the solution you  
suggested might work. I tried to install BackgrounDRb but was  
unsuccessful. I am working in Microsoft Windows platform. I ran the  
following command from my Rails app home

> ruby script/plugin install svn://rubyforge.org//var/svn/backgroundrb

No messages are shown. When I tried to run the next command then some  
errors are displayed:

> rake backgroundrb:setup  
> (in D:/Downloads/Ruby/RadRails/Workspace\_sample/Others/MT-Query Analyzer)  
> rake aborted!  
> Don’t know how to build task ‘backgroundrb:setup’

> (See full trace by running task with --trace)

How can I resolve this problem?

Ohhâ?¦ and there is an http proxy that restricts my internet access.

- Janeve George

---

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [September 1, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/5 "2006-09-01T18:08:52Z")

</div>

Hi~

On Aug 31, 2006, at 10:22 PM, Janeve George wrote:

> > a plugin for this you should check out here[1]  
> > errors are displayed:
> 
> Ohhâ?¦ and there is an http proxy that restricts my internet access.
> 
> - Janeve George

Hey Janave-

```
I think your proxy is just messing with svn and rubyforge. I have

```

had other win32 users report a similar problem behind a proxy. I am  
attaching a copy of the latest plugin to this email so you can get  
started. Unzip it and put it in vendor/plugins/backgroundrb . Then  
the setup and other tasks will work fine for you.

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![Janeve\_George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@Janeve\_George](https://www.ruby-forum.com/u/Janeve_George)\
**Post date:** [September 6, 2006, 8:20am UTC](https://www.ruby-forum.com/t/help-problems-with-multithreading-and-controller/66912/6 "2006-09-06T08:20:21Z")

</div>

Hi Ezra,

> I think your proxy is just messing with svn and rubyforge. I have  
> had other win32 users report a similar problem behind a proxy. I am  
> attaching a copy of the latest plugin to this email so you can get  
> started. Unzip it and put it in vendor/plugins/backgroundrb . Then  
> the setup and other tasks will work fine for you.

I am sorry but I am posting this message as a guest. I have not yet  
subscribed to this forum. If possible could you please resend the zip  
file to removed\_email\_address@domain.invalid. Sorry for this inconvenience.
