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

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~

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

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

backgroundrb.zip (23.6 KB)