Forum: Ruby on Rails Threads vs script/runner

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
kyle (Guest)
on 2005-11-29 21:16
(Received via mailing list)
I am currently running some background processes by using
script/runner to run an infinite loop (with a sleep).  I am
considering moving this to a thread that is forked off of the
environment file.  This would be easier to maintain, because I
wouldn't have to start and stop it independently of my rails app.  I
was wondering: Does anyone knows any more pros and cons of this

Kyle M.
brianvh (Guest)
on 2005-11-29 21:28
(Received via mailing list)
Wouldn't it be far better to set up a cron process that starts the
script/runner script on the period that you want? That would seem to be
of magnitude more efficient than running an infinite loop...

blair (Guest)
on 2005-11-29 21:40
(Received via mailing list)
The startup costs of script/runner are not insignificant, so a loop with
a sleep
is nice for the CPU costs, but probably harder to maintain.

zedshaw (Guest)
on 2005-11-30 09:15
(Received via mailing list)

The biggest problem with what you want is that normal Rails runs fairly
stateless, especially as a CGI.  With a CGI you'll never get it to
work.  I believe it'll work in WEBrick, but I remember that WEBrick
seemed to also kill off threads like Rails was a CGI.

Under FastCGI or SCGI you could probably do it since they are long
running processes, but you'll have to setup your thread(s) outside of
the Rails control, most likely with a global or similar (Singleton?)
unique place.

Another option--if you need better control from your Rails
application--is to create a simple DRb API to your stand-alone
processor.  This kind of gives you the best of all worlds.

The finaly problem I *know* you'll run into is if this processor is
supposed to be unique on a machine--where only one runs--then you'll
have huge problems in a clustering situation.  Imagine it this way:
You have your wonderful processor thread working great with 1 SCGI
process.  Now, you decide to speed up your app and start 4 SCGI
processor, which also means you have 4 of your little processing
threads running.

In this situation, look to the DRb solution or just a simple cronjob.

Zed A. Shaw

On Tue, 29 Nov 2005 11:14:34 -0800
This topic is locked and can not be replied to.