HELP! Problems with multithreading and Controller

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)?

> > 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

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

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

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

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 [email protected]. Sorry for this inconvenience.