Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I need your esteemed help on some wierd behaviour of alarms in ActivePerl. I am using v5.12.2 on Windows 2008 SP1 32bit.

I want to read from a long-running co-process until I am tired, then kill the co-process. I boiled down my code to the following snippet, replacing the real co-process with a dummy command:

#! /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++) {p +rint \"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; # for (my $i=0;$i<5000;$i++) {} } kill('INT', $procid) if $timeout;

I could not make the alarm work. I even tried unsafe signals. However, putting some tedious calculations somewhere inside the loop, will strangely solve the problem. Must this be considered as a bug?

I also experimented with threads and semaphores and finally found a work-around. But that would be an awful bunch of code for something quite obvious. Is there a state-of-the-art solution to this?

----------

Thank you, Monks, for all your helpful answers. I will have to study and try for a while to understand all the implications of your proposals. The next thing, I will probably do, is using Win32::Job and/or Win32::Process. These sound quite reasonable and easy to interface, although, alas, they will impair portability of my code.


In reply to Alarms with ActivePerl do not work properly by heiermann

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found