Using Ruby's “ready?” IO method with gets, puts, etc

佐手、 提交于 2019-12-24 00:23:58

问题


The standard Ruby library "io/wait" provides a method on IO objects ready? that returns non-nil if there is input available, nil or false otherwise. I know some methods like sysread and syswrite are not safe to use with higher level methods such as gets and read, and wanted to know if ready? was safe to mix with the higher level methods. The ready method seems rather useful and perhaps more elegant than relying on IO.select, but, surprisingly enough, I haven't seen it used much. There is nothing in the documentation to indicate it would be unsafe to use with read or gets, I just want to be sure I'm not mixing incompatible methods that could ultimately result in unexpected behavior when reading/writing sockets.

Edit: I suppose I should mention I am using Ruby 1.8.7.


回答1:


I don't have a lot of experience with Ruby, but I have a heck of a lot of experience with libc, and my opinion is that yes, it is safe. Odds are pretty good that "ready" is implemented in terms of select() or poll()...

If "ready" behaves like a select() that's been passed a timeval with zeros for tv_sec, and tv_usec, then the downside to "ready" is that you'll be spinning... Can you pass a timeout to ready?



来源:https://stackoverflow.com/questions/1779347/using-rubys-ready-io-method-with-gets-puts-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!