使用Ruby的“准备好了吗?”带get,puts等的IO方法 [英] Using Ruby's "ready?" IO method with gets, puts, etc

查看:131
本文介绍了使用Ruby的“准备好了吗?”带get,puts等的IO方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的Ruby库io / wait在IO对象 ready?上提供了一个方法,如果有可用的输入则返回非nil,否则返回nil或false。我知道一些方法,如 sysread syswrite 使用更高级别的方法是不安全的,例如获取读取,并想知道是否准备就绪?可以安全地混合使用更高层次的方法。 ready方法似乎相当有用,可能比依赖 IO.select 更优雅,但是,令人惊讶的是,我还没有看到它用得太多。 文档中没有任何内容表明它会使用读取获取是不安全的,我只是想确定我没有混合使用不兼容的方法在读/写套接字时最终导致意外行为。

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.

编辑:我想我应该提到我使用的是Ruby 1.8.7。

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

推荐答案

我对Ruby没有很多经验,但我对libc和我的经验很多意见是的,这是安全的。根据select()或poll()实现就绪的几率非常好......

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()...

如果就绪的行为类似于select()那么为tv_sec和tv_usec传递了一个带零的时间值,然后准备好的缺点就是你将要旋转......你能超时准备吗?

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?

这篇关于使用Ruby的“准备好了吗?”带get,puts等的IO方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆