Janeve George
2006-Aug-30 06:23 UTC
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
<form action=''tparse''>
<textarea name=''query''></textarea>
<input type=''submit''>
</form>
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)?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Max Muermann
2006-Aug-30 07:46 UTC
Re: HELP!!! Problems with multithreading and Controller...
<snip>> > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Ezra Zygmuntowicz
2006-Aug-30 08:13 UTC
Re: HELP!!! Problems with multithreading and Controller...
On Aug 30, 2006, at 12:46 AM, Max Muermann wrote:> > <snip> >> >> 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, > MaxAlso 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Janeve George
2006-Sep-01 05:22 UTC
Re: HELP!!! Problems with multithreading and Controller...
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/backgroundrbNo 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Ezra Zygmuntowicz
2006-Sep-01 16:06 UTC
Re: HELP!!! Problems with multithreading and Controller...
Hi~ On Aug 31, 2006, at 10:22 PM, Janeve George wrote:> > 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 GeorgeHey 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Janeve George
2006-Sep-06 06:20 UTC
Re: HELP!!! Problems with multithreading and Controller.
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 janeve.george-5IUUFp9WAf+zZ81WvyQhBw@public.gmane.org Sorry for this inconvenience. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---