Problems with IO.popen in ruby 1.8.2?

T

Todd A. Jacobs

I have the following code snippet:

sleep 0.3
IO.popen('/usr/bin/xclip -o').readlines {|f| print}

running in a tight loop. If I let it run for a short time (about 25
seconds), I get this error:

Enter URL: ./dice.rb:89:in `popen': Too many open files - /usr/bin/xclip -o (Errno::EMFILE)
from ./dice.rb:89:in `read'
from ./dice.rb:181
from ./dice.rb:162:in `loop'
from ./dice.rb:162

I don't understand the nature of the error, since the pipe is implicitly
closed when the block exits, right? Event if I change the line to:

IO.popen('/usr/bin/xclip -o').readlines {|f| print; f.close}

I still have the same problem. So, what file resource is being consumed,
and how do I fix it?
 
A

ara.t.howard

I have the following code snippet:

sleep 0.3
IO.popen('/usr/bin/xclip -o').readlines {|f| print}

running in a tight loop. If I let it run for a short time (about 25
seconds), I get this error:

Enter URL: ./dice.rb:89:in `popen': Too many open files - /usr/
bin/xclip -o (Errno::EMFILE)
from ./dice.rb:89:in `read'
from ./dice.rb:181
from ./dice.rb:162:in `loop'
from ./dice.rb:162

I don't understand the nature of the error, since the pipe is
implicitly
closed when the block exits, right? Event if I change the line to:

IO.popen('/usr/bin/xclip -o').readlines {|f| print; f.close}

I still have the same problem. So, what file resource is being
consumed,
and how do I fix it?

read carefully - you've given the block to readlines - not popen.
invert your logic

IO.popen(cmd) do |pipe|
pipe.readlines{|line| print line}
end


-a
 

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,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top