in reply to Alarms with ActivePerl do not work properly
The problem is that the signal emulation won't interrupt a blocking read. That's because the read runs in the kernel space, but the emulation runs in user space. It's a bug in the emulation, but not one that is easily corrected.
The following small mod makes it work well enough for most purposes:
#! /usr/bin/perl -w use strict; # Starting a co-process to read from. my $procid = open(READ, '-|', 'perl -e "$|=1; for ($i=0;$i<10;$i++) {print \"Line $i\n\"; sleep +1;}"' ); my $timeout = 0; $SIG{ALRM} = sub { $timeout = 1; }; alarm(3); # Reading from co-process. while (!$timeout) { my $line = <READ>; last unless defined $line; print $line; sleep 1; } kill('INT', $procid) if $timeout;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Alarms with ActivePerl do not work properly (small modification)
by tilly (Archbishop) on Jan 13, 2011 at 00:55 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2011 at 01:07 UTC | |
by tilly (Archbishop) on Jan 13, 2011 at 06:33 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2011 at 07:12 UTC | |
by Corion (Patriarch) on Jan 13, 2011 at 18:01 UTC | |
|
Re^2: Alarms with ActivePerl do not work properly (small modification)
by ikegami (Patriarch) on Jan 13, 2011 at 17:17 UTC | |
by BrowserUk (Patriarch) on Jan 13, 2011 at 19:15 UTC |