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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值