Block method to yield stdout?

L

List.rb

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

Ken Bloom

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
 
L

list. rb

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

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
 

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,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top