Using Open3 popen3 - handling stderr and stdin on a system call
September 10th, 2007
Problem:
Running a system call from an object in a Rails thread - if the process outputs to stderr or stdout, and you have not set up IO buffers to hold the bytes, the process will stall until it times out.
e.g.
def publish
...
@gallery = fetch_gallery()
system("script/generate gallery_creator #{gallery.id}")
...
end
If the generator outputs to stderr or stdout, the process will stall.
The solution is Open3 (thanks lachie).
def publish
...
@gallery = fetch_gallery()
Open3.popen3("script/generate gallery_creator #{@gallery.id}") do |stdin, stdout, stderr|
logger.info("stdout - #{stdout.readlines}")
logger.info("stderr - #{stderr.readlines}")
end
...
end
Leave a Reply