launch external program

A

Andrey Demidov

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.
 
C

Christopher Carver

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

system("command")

You cannot save the output, though.
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:
system("command")

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
 
R

Robert Klemme

Am Mittwoch, 17. Jun 2009, 22:22:49 +0900 schrieb Christopher Carver:

Christopher, system does not execute commands asynchronously.
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
 
J

Jeff Bowen

Joel said:
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?
 
R

Robert Klemme

2009/6/17 Joel VanderWerf said:
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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 18. Jun 2009, 05:45:08 +0900 schrieb Robert Klemme:
You can do this as well:

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

Gosh, that's the programming style I was looking for.

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top