Multithreaded action


#1

I have an action in a controller that generates and returns a custom
png-file, it goes something like this:

def custompng
#1 gather some userdata from params.
#2 make a system call to an advanced java application that renders a
png based on the custom data. stores the png on disk.
#3 read the png into rails
#4 send the png to the browser with method send_data
#5 delete the png from disk
end

The problem is that the png-rendering takes very long time, ~2 seconds,
and consumes a lot of the cpu resources. During this time the rails app
become totaly locked up, not handling any incoming requests from other
visitors.

I would like to have this method run in its on lower priority thread. Is
that possible?

I have tried to put the system call in a thread but that that didnt
work, the main execution seemed to go on when the png was generating,
resulting in a file not found error when trying to do step #3. And
putting all steps in the thread block also didnt work, the png data was
never received by the browser.

Any thoughts on how I can solve this problem?


#2

On May 20, 2006, at 3:26 AM, Daniel wrote:

#5 delete the png from disk

thread. Is
that possible?

By ‘system call’ I presume you mean Kernel#` or Kernel#system? If
so, changing the priority of the ruby thread won’t do you any good.
Use nice(1) instead.

I have tried to put the system call in a thread but that that didnt
work, the main execution seemed to go on when the png was generating,
resulting in a file not found error when trying to do step #3. And
putting all steps in the thread block also didnt work, the png data
was
never received by the browser.

Any thoughts on how I can solve this problem?

Try BackgroundDRb.


Eric H. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com


#3

You need your thread to finish processing before you can load the image
data. If your Thread.new call had its return value stored in
the_thread,
run the_thread.join to wait for it to complete before continuing
execution.


#4

Eric H. wrote:

By ‘system call’ I presume you mean Kernel#` or Kernel#system? If
so, changing the priority of the ruby thread won’t do you any good.
Use nice(1) instead.

by system call I mean:

system(“java -jar myjarfile.jar”)

Try BackgroundDRb.

Thank you for the input. I will do some research in BackgroundDRb and
Kernel.
Hopefully my problem will be solved…

//Daniel