Forum: Ruby on Rails multithreaded action

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6738e5c4f17822f7e163ba938344755d?d=identicon&s=25 Daniel (Guest)
on 2006-05-20 12:26
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?
E10a35d5f2a85c5f65fa848eaac48859?d=identicon&s=25 Zack Ham (Guest)
on 2006-05-20 18:45
(Received via mailing list)
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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-22 20:41
(Received via mailing list)
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 Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
6738e5c4f17822f7e163ba938344755d?d=identicon&s=25 Daniel (Guest)
on 2006-05-29 15:40
Eric Hodel 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
This topic is locked and can not be replied to.