简体   繁体   English

如何使用IO :: Select阻止等待读或写套接字?

[英]How can I block waiting for either a read or write socket using IO::Select?

I'm using IO::Select . 我正在使用IO :: Select

It is an OOP wrapper for the normal select call , but it exposes can_read() and can_write() methods. 它是普通select调用的OOP包装器,但它公开了can_read()can_write()方法。 Is there no way to block waiting for a read or write socket? 有没有办法阻止等待读或写套接字? Does this not (almost) defeat the purpose of having IO::Select in the first place? 这是否(几乎)没有打败拥有IO :: Select的目的?

Use IO::Select->select : 使用IO::Select->select

select ( READ, WRITE, EXCEPTION [, TIMEOUT ] ) 选择(READ,WRITE,EXCEPTION [,TIMEOUT])

select is a static method, that is you call it with the package name like new . select是一个静态方法,即使用包名称(如new调用它。 READ , WRITE and EXCEPTION are either undef or IO::Select objects. READWRITEEXCEPTIONundefIO::Select对象。 TIMEOUT is optional and has the same effect as for the core select call. TIMEOUT是可选的,其作用与核心选择调用相同。

The result will be an array of 3 elements, each a reference to an array which will hold the handles that are ready for reading, writing and have exceptions respectively. 结果将是一个由3个元素组成的数组,每个元素都是对数组的引用,该数组将保存准备好读取,写入和分别具有异常的句柄。 Upon error an empty list is returned. 出现错误时,将返回一个空列表。

For example: 例如:

my $rsel  = IO::Select->new($rfh);
my $wsel  = IO::Select->new($wfh);
my $esel  = IO::Select->new($rfh, $wfh);

my @ready = IO::Select->select($rsel, $wsel, $esel, undef);

If TIMEOUT is not given and any handles are registered then the call will block. 如果未给出TIMEOUT且未注册任何句柄,则调用将阻塞。

It looks like can_read and can_write will block if you don't pass a timeout. 如果您不通过超时, can_readcan_write 阻塞。 I'd suggest that this is rarely what is desired, though it might be under some circumstances. 我建议这虽然在某些情况下可能很少需要。

Personally, I'd suggest using one of the many event modules (POE, AnyEvent, etc) instead. 就个人而言,我建议改用许多事件模块之一(POE,AnyEvent等)。 It's a bit more to get going, but handles more scenarios more cleanly. 要进行的工作还多一些,但可以更轻松地处理更多方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM