krmx has asked for the wisdom of the Perl Monks concerning the following question:

Im trying to understand why the following code wont run correctly, when Im using pause instead of sleep and WWW::Curl. This code will loop two times only and it will get stuck no matter how long I will wait. This happens in Linux, FreeBSD. Stuck here means, it does not continue the loop, seems stuck in pause ... Same construct, alarm,pause works fine if I dont use WWW::Curl, for other types of work.
use strict; use warnings; use WWW::Curl::Easy; use POSIX qw(pause); use Time::HiRes qw(time alarm setitimer sleep ITIMER_REAL); use Getopt::Std; ### Process command line args usage() if defined $ARGV[0] and $ARGV[0] eq "--help"; getopts('hvV') or usage(); # process [interval [count]], my ( $interval, $loop_max ); if ( defined $ARGV[0] ) { $interval = $ARGV[0]; $loop_max = defined $ARGV[1] ? $ARGV[1] : 2**32; usage() if $interval == 0; } else { $interval = 1; $loop_max = 1; } ### Variables my $loop = 0; # current loop number # how often do we trigger (seconds)? my $first_interval = $interval; # signal handler is empty $SIG{ALRM} = sub { }; # first value is the initial wait, second is the wait thereafter setitimer( ITIMER_REAL, $first_interval, $interval ); my $http = WWW::Curl::Easy->new(); if (&WWW::Curl::Easy::version() !~ /ssl|nss/i) { die "No SSL support"; } # Get the results while (1) { my $url = "http://kronometrix.org/"; $http->setopt(CURLOPT_URL, $url); my $retcode = $http->perform(); ## Get the results my $response = $http->getinfo(CURLINFO_HTTP_CODE); if ($retcode == 0) { print "Ok, Status: $response\n"; } else { print "Error, Status: $response\n"; } ### Check for end last if ++$loop == $loop_max; ### Interval pause; # sleep $interval; }

and if I replace pause with sleep this works fine. Im using this alarms with timers to trigger certain actions at specific time intervals.

Anyone any ideas why WWW::Curl breaks down on pause or am I using this wrong ?. Many thanks for help.

Replies are listed 'Best First'.
Re: WWW::Curl pause conflict
by Anonymous Monk on Sep 20, 2015 at 23:47 UTC

    From reading pause, you shouldn't be using pause, you should be using sleep

      Additional: I was wrong - using sleep instead of pause does not work. there is no difference whatsoever between:
      while (1) { my $url = "http://kronometrix.org/"; $http->setopt(CURLOPT_URL, $url); my $retcode = $http->perform(); ## Get the results my $response = $http->getinfo(CURLINFO_HTTP_CODE); if ($retcode == 0) { print "Ok, Status: $response\n"; } else { print "Error, Status: $response\n"; } ### Check for end last if ++$loop == $loop_max; ### Interval pause; }
      and
      while (1) { my $url = "http://kronometrix.org/"; $http->setopt(CURLOPT_URL, $url); my $retcode = $http->perform(); ## Get the results my $response = $http->getinfo(CURLINFO_HTTP_CODE); if ($retcode == 0) { print "Ok, Status: $response\n"; } else { print "Error, Status: $response\n"; } ### Check for end last if ++$loop == $loop_max; ### Interval sleep; }
      The code gets stuck after 2 run samples. Always 2 . not sure why. It seems to me something between WWW::Culr (libcurl) and perl regarding alarms and timers.
      I understand that it is not safe to use sleep and a timer at the same time within Perl code. My sample uses POSIX timers and alarm. https://www.febo.com/pages/perl_alarm_code/ At the same time: this code works fine for other types of work, like here: https://github.com/kronometrix/recording/blob/master/bin/linux/cpurec
Re: WWW::Curl pause conflict
by Anonymous Monk on Sep 21, 2015 at 18:46 UTC
    Maybe the handle goes away somehow when you pause. What if you obtained www::curl each time in the loop?