[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Starting a process from inside Ruby

Petr Janda

2/19/2009 1:10:00 AM

Hi all,
Im facing a small dilema trying to start a process called "ezstream"
from inside a ruby on rails application and retreive its PID.

Ezstream is used for streaming audio into an icecast server so normally
this process runs in background for a long time. I tried it with
IO.popen but then the application hangs indefinitely. What I need to do
is start the app, get its PID(to store it in a databasse), and move it
to the background so the rails application doesnt hang.

Any ideas?
Petr
--
Posted via http://www.ruby-....

6 Answers

Michael Malone

2/19/2009 1:18:00 AM

0

Petr Janda wrote:
> Hi all,
> Im facing a small dilema trying to start a process called "ezstream"
> from inside a ruby on rails application and retreive its PID.
>
> Ezstream is used for streaming audio into an icecast server so normally
> this process runs in background for a long time. I tried it with
> IO.popen but then the application hangs indefinitely. What I need to do
> is start the app, get its PID(to store it in a databasse), and move it
> to the background so the rails application doesnt hang.
>
> Any ideas?
> Petr
>
I don't know if this works inside rails but the Kernel#fork method is a
good idea!

pid = fork() do
#Ezstream stuff
end
#store pid in db
...
wait(pid)

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================


Jayce Meade

2/19/2009 3:20:00 AM

0

Fork would work from what I've read, I believe it starts the other process
and continues execution of the current ruby script, the external process
runs until it is (from what I understand) terminated by the parent script,
normal exit, and the like.

This should help, it covers the basic process management... features?
provided by ruby, including threads, system(), exec(), fork(), etc..

http://andrejserafim.wordpress.com/2007/12/16/multiple-threads-and-processe...

On this subject, when I use fork, I get an error saying it hasn't been
implemented, how would I fix this?

- jayce
--------------------------------------------------
From: "Michael Malone" <michael.malone@tait.co.nz>
Sent: Wednesday, February 18, 2009 5:18 PM
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: Re: Starting a process from inside Ruby

> Petr Janda wrote:
>> Hi all,
>> Im facing a small dilema trying to start a process called "ezstream"
>> from inside a ruby on rails application and retreive its PID.
>>
>> Ezstream is used for streaming audio into an icecast server so normally
>> this process runs in background for a long time. I tried it with
>> IO.popen but then the application hangs indefinitely. What I need to do
>> is start the app, get its PID(to store it in a databasse), and move it
>> to the background so the rails application doesnt hang.
>>
>> Any ideas?
>> Petr
>>
> I don't know if this works inside rails but the Kernel#fork method is a
> good idea!
>
> pid = fork() do
> #Ezstream stuff
> end
> #store pid in db
> ...
> wait(pid)
>
> =======================================================================
> This email, including any attachments, is only for the intended
> addressee. It is subject to copyright, is confidential and may be
> the subject of legal or other privilege, none of which is waived or
> lost by reason of this transmission.
> If the receiver is not the intended addressee, please accept our
> apologies, notify us by return, delete all copies and perform no
> other act on the email.
> Unfortunately, we cannot warrant that the email has not been
> altered or corrupted during transmission.
> =======================================================================
>
>
>

David Masover

2/19/2009 6:00:00 AM

0

Petr Janda wrote:
> What I need to do
> is start the app, get its PID(to store it in a databasse), and move it
> to the background so the rails application doesnt hang.
>

I think the other posts are right... If it's not part of your app, by
the way, you could do:

pid = fork { exec('ezstream') }

Assuming 'ezstream' is the command you'd have to run.

> Any ideas?

Well, since you asked...

This problem has been solved over and over again in Rails. Look up
workling/starling, nanite (or whatever it's called now), backgroundrb,
drb, monit, god, etc, ad nauseum.

I'm not saying that there's no possibility of doing it better -- I know
I had a similar problem recently that I solved with an old-fashioned
daemon and polling. Just probably worth looking at what's already out
there before you reinvent the wheel.

Petr Janda

2/19/2009 1:02:00 PM

0


> pid = fork { exec('ezstream') }
>
> Assuming 'ezstream' is the command you'd have to run.
>


Hmm there seems to be an issue running this code in rails as the 2nd
process never seems to start although ezstream does get started. It also
produces Application Error message.
--
Posted via http://www.ruby-....

Petr Janda

2/19/2009 1:24:00 PM

0

Ok ive had some success with using threads instead of forks. Im able to
retreive the pid of the ezstream easily process because Threads share
variables!

Thanks.
Petr
--
Posted via http://www.ruby-....

Rick DeNatale

2/19/2009 5:27:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Feb 19, 2009 at 8:23 AM, Petr Janda <elekktretterr@exemail.com.au>wrote:

> Ok ive had some success with using threads instead of forks. Im able to
> retreive the pid of the ezstream easily process because Threads share
> variables!
>

You might also want to have a look at backgroundrb which is the normal way
to do background jobs in Rails.

http://backgroundrb.ruby...

--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...