Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Wakeup: event-loop system

by fernandes (Monk)
on Mar 01, 2009 at 00:19 UTC ( [id://747231]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

Having nothing but Perl to wakeup I wrote the folwoing bad code. Zentara told me about the event-loop gap caused by nesting while:
#!/usr/bin/perl -w use strict; system("clear"); desperta{ #en: wakeup my $tempo_inicial = time; my $tempo_limite = shift; while (time<($tempo_inicial+$tempo_limite)){ print "Enquanto resta ", -(time-($tempo_inicial+$tempo_limite)), " + segundos para acordar, porque não dormes?\n"; #en: 'Why don't you sleep while you have ', time, ' second(s) for +that?' my $tempo_inicial_interno = time; while (time<($tempo_inicial_interno+1)){ #não faz nada enquanto não transcorrer 1 segundo #en: waits 1 second }; system("clear"); }; while(1){ $a=1; while($a<1500){ #print $a, "\n"; print "ACORDA "; #en: WAKEUP $a++; } print chr hex 7; }; }; 1;

Obviously Zentara is right. This piece of perl consumes almost all the processors clock.

Would you be interested in giving me insightful alternatives for helping me to get the train?

Warming regards!

Replies are listed 'Best First'.
Re: Wakeup: event-loop system
by GrandFather (Saint) on Mar 01, 2009 at 02:11 UTC

    Replace:

    while (time<($tempo_inicial_interno+1)){ #não faz nada enquanto não transcorrer 1 segundo #en: waits 1 second };

    with:

    sleep 1;

    See sleep


    True laziness is hard work
      Thank you! Sleep 1 has decreased about 50% of clock consumption.
      Do you think the code is ok now?

        A similar solution is required fro your second loop - put a sleep in it. As it stands the loop will run just as fast as it can output stuff with print. That will be slower than an empty loop, but still pretty fast.

        Another issue is that there is no way of stopping the 'alarm' loop. At the very least you should replace the while (1) with a down counter so it stops by itself eventually.


        True laziness is hard work
Re: Wakeup: event-loop system
by sundialsvc4 (Abbot) on Mar 01, 2009 at 05:46 UTC

    When a program “has nothing to do,” you always want it to explicitly give up control of the CPU, so that other processes can use the CPU. It can sleep for some time-interval, which will give up the CPU during that time, but if it needs to wait “until something happens” then you need to look at things like: signals, semaphores, events, and so-on. With these, the CPU can ignore your program until it truly needs to use the CPU again... an hour from now or a millisecond from now... then resume running it promptly. So the CPU doesn't waste it's time.

    Example:   in real life, you don't have to constantly pick up the telephone to discover if someone is trying to talk to you. It rings, and (only) then you respond. At all other times you can ignore the phone, and you still won't miss calls.

    In any “event-loop” system, the program goes to sleep until the next event arrives. It consumes no CPU resources at all until it has another event to process.

      You have been very clear on your explanation of "event-loop" "physiology". The telephone metaphor is astonishing insightful for me.
      Thank you.
Re: Wakeup: event-loop system
by zentara (Archbishop) on Mar 01, 2009 at 15:26 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found