[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Block method to yield stdout?

list. rb

10/20/2008 6:20:00 PM

All, I've written a distribution service that spawns processes using
IO.popen in a seperate thread for each job.

In my Task class, I would like to create a block method such as
'Task.monitor(mode="stdout")', which would yield the stdout for that
task from that point forward. -- similar to tail -f, but for a
multithreaded environment.

The result would in essence be called like:

task = Task.new(:cmd => "tail -f /some/log")
sleep 10
task.monitor
# fresh content from some/log will be yielded


Since $stdout is global, what would be the beat way tackle this?

3 Answers

Ken Bloom

10/20/2008 8:28:00 PM

0

On Mon, 20 Oct 2008 13:19:56 -0500, List.rb wrote:

> All, I've written a distribution service that spawns processes using
> IO.popen in a seperate thread for each job.
>
> In my Task class, I would like to create a block method such as
> 'Task.monitor(mode="stdout")', which would yield the stdout for that
> task from that point forward. -- similar to tail -f, but for a
> multithreaded environment.
>
> The result would in essence be called like:
>
> task = Task.new(:cmd => "tail -f /some/log") sleep 10
> task.monitor
> # fresh content from some/log will be yielded
>
>
> Since $stdout is global, what would be the beat way tackle this?

Maybe use Open3, or popen. Worst case, look at how Open3 does a fork/exec
to call the appropriate program.

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Mark Thomas

10/20/2008 9:12:00 PM

0

On Oct 20, 2:19 pm, "List.rb" <list...@gmail.com> wrote:

> In my Task class, I would like to create a block method such as  
> 'Task.monitor(mode="stdout")', which would yield the stdout for that  
> task from that point forward. -- similar to tail -f

Have you seen File::Tail?
http://file-tail.rubyforge.org/doc/...

-- Mark.

list. rb

10/21/2008 2:42:00 AM

0

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

On Mon, Oct 20, 2008 at 5:13 PM, Mark Thomas <mark@thomaszone.com> wrote:

> On Oct 20, 2:19 pm, "List.rb" <list...@gmail.com> wrote:
>
> > In my Task class, I would like to create a block method such as
> > 'Task.monitor(mode="stdout")', which would yield the stdout for that
> > task from that point forward. -- similar to tail -f
>
> Have you seen File::Tail?
> http://file-tail.rubyforge.org/doc/...
>
> -- Mark.
>
>
Thanks Mark for replying. Yes I have used Tail before but what I'm looking
for is more along the lines of a non-blocking version of IO#readlines

Currently, I do this on the server side:

def spawn(job)
task = Task.new(job)
queue = Queue.new
Thread.new(queue) {|q|
pipe = IO.popen(task.job.cmd)
q.push(pipe)
q.push(pipe.pid)
task.pid = pipe.pid
begin
Thread.new {
task.output = pipe.readlines
pipe.close
task.exitstatus = $?.exitstatus
task.history << {:completed => Time.now}
}
rescue => e
q.push e
end
}
queue.clear
self.tasks.push(task)
return task
end

Since IO#readlines blocks, I am not sure how to tap into the process output
:-(

Thanks again