3

Looks like times of old good alarm() call are over, so how to interrupt blocking read()'s and write()'s made from perl thread (using that brand new 'threads' module) assuming the code making those blocking calls cannot be changed? Actual problem is stucking communication with Modbus device so I've created a simple testcase not to dip you into RS-485 hell:

use threads;
use IO::Handle;
threads->create(sub {
        $io = IO::Handle->new_from_fd(fileno(STDIN), 'r') or die;
        $br = read $io, $buf, 100;
        warn "read: $br";
});
while(1) {threads->yield()};

Here, warn() is never executed unless you hit Ctrl-D on keyboard. Is there any simple solution to timeout that read() call?

4

1 回答 1

1

而不是线程(这在 Perl 中充其量是不支持的,而且大多不赞成),为什么不尝试其中一个事件模块呢?这是 Perl 中并发的标准解决方案。

于 2012-07-23T16:59:11.393 回答