in reply to GPS Widget

In addition, the better threads module has a means to kill a thread, $thr->kill('SIGUSR1'); which may also work for you.

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: GPS Widget
by deadpickle (Pilgrim) on Feb 22, 2008 at 01:41 UTC
    I emailed the author of the GPS-Perl module and he suggested to use the alarm function. so I tried it out with the code below but, it still hung on the get_position so maybe perlmonks has a suggestion.
    sub gps { my $gps_device; while (1) { goto END if $die == 1; if ($gps_start == 1) { $gps_device = GPS::NMEA->new(Port => $gps, Baud => 4800) o +r die; my $timeout = 5; while (1){ #stops the warnings in the GPS device module local $^W = 0; print "before\n"; eval { local $SIG{ALRM} = sub {die "GPS Device has timed +out\n"}; alarm $timeout; ($ns,$lat,$ew,$lon) = $gps_device->get_position; #warn $@ if $@; alarm 0; }; die if $@ && $@ ne "GPS Device has timed out\n"; if ($@){ print "WARNING\n"; } else{ print "nope\n"; } #($ns,$lat,$ew,$lon) = $gps_device->get_position; print "after\n"; print "$die\n"; goto END if $die == 1; last if $gps_start == 0; sleep 3; } undef $gps_device; } else {sleep 4} } END: }

      You could try setting the environment var set PERL_SIGNALS=unsafe prior to running your script and see if that will allow alarm to interupt the underlying IO call. I think this has to be set prior to perl being run, but I wouldn't swear to it.

      See Safe signals for (a little) explanation of what this does.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      From my recollection, alarm won't work correctly in a thread on linux, the parent thread gets all the alarms. So you might be able to collect the SIG{ALRM} in the parent thread, and then send the signal to the thread. Or you may be able to find another workaround, like a separate timer thread, which times the gps sub.

      Another workaround I've seen, is to eval a fork-&-exec in the thread, and you can get the alarm to work in the child pid. If I recall correctly...... :-)

      Personnally I would use a separate timer thread, and use shared variables to somehow communicate and handle the timeout.


      I'm not really a human, but I play one on earth. Cogito ergo sum a bum