SPUG: Pull out of a blocking subroutine?

Christopher Howard choward at indicium.us
Mon Jan 11 18:19:24 PST 2010


Skylos wrote:
> Sure. 
> 
> http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlfunc/alarm.html
> 
>     eval {
>         local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
>         alarm $timeout;
>         $nread = sysread SOCKET, $buffer, $size;
>         alarm 0;
>     };
>     if ($@) {
>         die unless $@ eq "alarm\n";   # propagate unexpected errors
>         # timed out
>     }
>     else {
>         # didn't
>     }
> 
> Skylos
> 
> "If only I could get rid of hunger by rubbing my belly" - Diogenes
> 
> 
> On Mon, Jan 11, 2010 at 5:27 PM, Christopher Howard <choward at indicium.us
> <mailto:choward at indicium.us>> wrote:
> 
>     Hi. I'm still working on integrating LEGO::NXT into my project. Problem
>     I've hit though is that apparently there are no actual functions for
>     checking communication status with the robot.
> 
>     There is other function I thought I could use to detect (implicitly) if
>     communication is still established. Problem is that they are /all/
>     blocking. Documentation basically says that the author hasn't got around
>     to making non-blocking functions yet, and I don't really have time to
>     reprogram his module myself.
> 
>     So, my perverted, evil question: Is there some hackish way to pull out
>     of a blocking subroutine? Say, if it has returned after three seconds or
>     something like that?
> 
>     --
>     Christopher Howard
>     http://indicium.us
>     http://theologia.indicium.us
>     http://robots.arsc.edu
> 
> 
>     _____________________________________________________________
>     Seattle Perl Users Group Mailing List
>         POST TO: spug-list at pm.org <mailto:spug-list at pm.org>
>     SUBSCRIPTION: http://mail.pm.org/mailman/listinfo/spug-list
>        MEETINGS: 3rd Tuesdays
>        WEB PAGE: http://seattleperl.org/
> 
> 

This sounds like exactly what I need, but it doesn't seem to be working.
 It still freezes at the call to Does it make any difference that I am
not making a simple system call, but instead a call to a function in a
module?

while(1)
{
    eval {

        local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required


        alarm 5;
	# $Scoop_a is my robot object

        $ret =
$Scoop_a->nxt->get_battery_level($LEGO::NXT::Constants::NXT_RET);
        alarm 0;
    };
    if ($@) {
        die unless $@ eq "alarm\n";   # propagate unexpected errors


        # timed out


    }
    else {
        # didn't


    }
    print "We made it through!";
    sleep 2;
}

-- 
Christopher Howard
http://indicium.us
http://theologia.indicium.us
http://robots.arsc.edu

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 261 bytes
Desc: OpenPGP digital signature
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20100111/db57921e/attachment.bin>


More information about the spug-list mailing list