Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: inconsistent eval timeout

by vr (Curate)
on Oct 31, 2019 at 18:48 UTC ( [id://11108191]=note: print w/replies, xml ) Need Help??


in reply to inconsistent eval timeout

"OPTION 2" doesn't timeout at all, it waits for external command to complete normally. I think because you used backticks instead of system, Perl didn't die in 3 sec but waited until STDOUT of spawned process was closed. This should work:

use strict; use warnings; my $timeout_msg = "Timeout after 3 seconds!\n"; my $t = time; my $pid; eval { local $SIG{ ALRM } = sub { die $timeout_msg }; alarm 3; $pid = system 1, 'timeout /t 6 > nul && echo Good Morning!'; waitpid $pid, 0; alarm 0 }; if ( $@ ) { die $@ unless $@ eq $timeout_msg; print $@; kill 'KILL', $pid } print time - $t, "\n";

I hope my use of timeout command won't confuse you to insert it somewhere in your code:), it's just to emulate long-running process. More important is Perl Windows system extension.

Replies are listed 'Best First'.
Re^2: inconsistent eval timeout
by Sanjay (Sexton) on Nov 04, 2019 at 15:45 UTC

    Thanks. Works. However, the job keeps running in the background. Is the kill in the code one of the GNU Windows utilities?

    The & Good Morning! confused me. Why is it there? Presumable a dummy activity, but why?

    Instead of kill, on Windows I found the following more effective

    system("taskkill /F /T /PID $pid");

    The /F to force and the /T for all child processes.

    Please do explain the ... && Good ..., if convenient

      Thanks. Works. However, the job keeps running in the background. Is the kill in the code one of the GNU Windows utilities?

      no, kill is Perl's built-in, used to send any supported signal. Under Win32, it is described to "terminate the process identified by $pid, and make it exit immediately". Apparently, immediate exit is not guaranteed for unresponsive applications, so "taskkill /f" was required in your case.

      The & Good Morning! confused me. Why is it there? Presumable a dummy activity, but why?

      Correct, that "echo" part is kind of dummy; single/double (not important in this case) ampersand is to have several commands on one line, and serves no other purpose but to avoid checking for active tasks. Either I observe a "Good Morining!" even after Perl quit (child shell process was not terminated), or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found