`
ericluo
  • 浏览: 17707 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Fwd: Re: Exececuting another application from within ruby

阅读更多

-------------------- Start of forwarded message --------------------
From: "Ara.T.Howard" <Ara.T.Howard@noaa.gov>
Newsgroups: comp.lang.ruby
Subject: Re: Exececuting another application from within ruby
Date: Tue, 5 Jul 2005 07:03:40 +0900

On Tue, 5 Jul 2005, Nigel Wilkinson wrote:

> Hi folks
>
> I have a smell application from which I wish to display a file using an
> external helper programme.
>
> Lets say
> app = the external programme
> file = the file to be displayed
>
> I've tried
>
> system(app, file)
>
> This works but it blocks my application. How can I fire up the helper
> application in the background?

def background command
Thread::new(command, Thread::current) do |cmd, cur|
begin
pipe = IO::popen cmd
true while pipe.gets and pipe.close
$?.exitstatus
rescue Exception => e
cur.raise e
end
end
end

thread = background 'long-running'

...
...
...

p thread.value # exitstatus

hth. if you need control over stdout and stderr check out session. it
provides thread safe execution.

require 'session'

def background command
sh = Session::new
Thread::new(command, Thread::current) do |cmd, cur|
begin
stdout, stderr = sh.execute cmd
[stdout, stderr, sh.status]
rescue Exception => e
cur.raise e
end
end
end

thread = background 'long-running'

...
...
...

p thread.value # [stdout, stderr, exitstatus]

session also allows thread safe stuff like

def background command, widget
sh = Session::new
Thread::new(command, Thread::current) do |cmd, cur|
begin
sh.execute(cmd) do |stdout, stderr|
widget.update stdout, stderr
end
sh.status
rescue Exception => e
cur.raise e
end
end
end


hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================




-------------------- End of forwarded message --------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics