Forum: Ruby launch external program

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.
F579582123947e61e028061975fdee77?d=identicon&s=25 Andrey Demidov (demas)
on 2009-06-17 14:08
Hi all,

   I'm running external programs like this:

      status = `rdesktop .... &`

   But my ruby script doesn't work until the external program finished.
How can I run an external program asynchronously?

Thanks.
69a1c06744b28a4251263ed41cc909d7?d=identicon&s=25 Christopher Carver (Guest)
on 2009-06-17 15:23
(Received via mailing list)
system("command")

You cannot save the output, though.
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-06-17 16:03
(Received via mailing list)
Hi,

Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:
> You cannot save the output, though.
Yes, you can.

  output = ""
  t = Thread.new { output << `somecmd` }
  ...
  t.alive? or puts "The full output is:", output

Bertram
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-06-17 22:45
(Received via mailing list)
On 17.06.2009 16:02, Bertram Scharpf wrote:
> Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:
>> On Wed, Jun 17, 2009 at 8:08 AM, Andrey Demidov <andrey.demidov@gmail.com>wrote:
>>>   I'm running external programs like this:
>>>
>>>      status = `rdesktop .... &`
>>>
>>>   But my ruby script doesn't work until the external program finished.
>>> How can I run an external program asynchronously?
>>>
>> system("command")

Christopher, system does not execute commands asynchronously.

>> You cannot save the output, though.
>
> Yes, you can.

Obama? :-)

>   output = ""
>   t = Thread.new { output << `somecmd` }
>   ...
>   t.alive? or puts "The full output is:", output

You can do this as well:

t = Thread.new { `somecmd` }
...
output = t.value

It seems Thread#value is rarely used but it can be of very useful.

http://ruby-doc.org/core-1.8.7/classes/Thread.html#M000243

Kind regards

  robert
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-06-17 22:57
(Received via mailing list)
Robert Klemme wrote:
> output = t.value
Thread#value blocks while the thread is running, though.
7070c0ca59124b4d96dd27ea52ad359e?d=identicon&s=25 Jeff Bowen (jabowen)
on 2009-06-18 03:55
Joel VanderWerf wrote:
> Robert Klemme wrote:
>> output = t.value
> Thread#value blocks while the thread is running, though.

Ok but how would you in Windows start a seperate program like a vendor
software that runs seperate from the ruby program?  If I try
`run_me.exe` or
system("run_me.exe") the ruby program just sits waiting for the other to
finsh?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-06-18 10:08
(Received via mailing list)
2009/6/17 Joel VanderWerf <vjoel@path.berkeley.edu>:
>>
>> t = Thread.new { `somecmd` }
>> ...
>> output = t.value
>
> Thread#value blocks while the thread is running, though.

Well, yes.  But what is your point?  If you want the result you need
to wait anyway.  If not you can still use your pattern:

t.alive? or puts "The full output is:", t.value

Kind regards

robert
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-06-18 12:45
(Received via mailing list)
Hi,

Am Donnerstag, 18. Jun 2009, 05:45:08 +0900 schrieb Robert Klemme:
> output = t.value
Gosh, that's the programming style I was looking for.

Bertram
This topic is locked and can not be replied to.