looping every x seconds

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default looping every x seconds


    Hello,

    In PERL how do I execute a block of code every "x" seconds?
    I'm talking a loop or similar but it runs every x seconds, where x is given.
    kernal32 Guest

  2. Similar Questions and Discussions

    1. Quotes that appear every few seconds
      I am using dreamweaver and want to create a small side window on the page where short text quotes appear. The quotes need to change every 5-10...
    2. COM+ times out after 30 seconds.
      I have a web service, that calls a COM + ( Queued). The COM+ runs a stored procedure in Sql Server. Now this strored procedure is to run for 6 hrs...
    3. 2 seconds
      hiya! check this: only the banks know about this, but it will save you a fortune are you prepared for lower mortgage repayments? ...
    4. ls showing seconds?
      Hi all! Is there any way that ls show timestamps with seconds (i.e. format hh:mm:ss)? If not ls, maybe there is another command... ...
    5. format # of seconds to hh:mm
      I am trying to trick some Infromix SQL command, so it will format the output of a query into the HH:MM. Say, a row data value 70 will be print as...
  3. #2

    Default Re: looping every x seconds

    > Hello,
    >
    > In PERL how do I execute a block of code every "x" seconds?
    > I'm talking a loop or similar but it runs every x seconds, where x is
    > given.
    Sorry I didnt think I was clear.
    I'm using sleep 5 to wait 5 second before executing some code.

    my question is, how can I keep the rest of the program running whilst the
    loop continues in pararel
    kernal32 Guest

  4. #3

    Default Re: looping every x seconds

    kernal32 wrote:
    > In PERL how do I execute a block of code every "x" seconds?
    > I'm talking a loop or similar but it runs every x seconds,
    > where x is given.

    If "x" is not given, you cannot do this, yes?

    #!perl

    print "Press Control C To Stop Looping\n";

    while (1)
    {
    print "Purl Gurl Rocks And Rolls!\r";
    sleep (1);
    }


    Purl Gurl
    Purl Gurl Guest

  5. #4

    Default Re: looping every x seconds

    On Wed, 10 Sep 2003 10:57:28 +0800
    kernal32 <surakshan@removethisYahooRemove.com> wrote:
    > > In PERL how do I execute a block of code every "x" seconds?
    > > I'm talking a loop or similar but it runs every x seconds, where x
    > > is given.
    >
    > Sorry I didnt think I was clear.
    > I'm using sleep 5 to wait 5 second before executing some code.
    >
    > my question is, how can I keep the rest of the program running
    > whilst the loop continues in pararel
    Use fork. That's just one suggestion.

    ==untested==
    #!/usr/bin/perl -w
    use strict;

    my $child = fork();
    die "Can't fork: $!\n" unless defined $child;

    if($child > 0){
    #parent process
    }else{
    $child process
    }


    You'll need to add some signal handlers and maybe use some POSIX
    fuctionality. Basically, 'salt to taste' ;)

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. [url]http://www.gnu.org/licenses/gpl.txt[/url]
    for more information.

    a fortune quote ...
    If you think the United States has stood still, who built the
    <largest shopping center in the world? -- Richard M. Nixon
    James Willmore Guest

  6. #5

    Default Re: looping every x seconds

    On Wed, 10 Sep 2003 05:01:54 GMT
    James Willmore <jwillmore@cyberia.com> wrote:
    > $child process
    should be:
    #child process

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. [url]http://www.gnu.org/licenses/gpl.txt[/url]
    for more information.

    a fortune quote ...
    Only adults have difficulty with childproof caps.

    James Willmore Guest

  7. #6

    Default Re: looping every x seconds

    >>>>> "kernal32" == kernal32 <surakshan@removethisYahooRemove.com> writes:

    kernal32> I'm using sleep 5 to wait 5 second before executing some
    kernal32> code.

    kernal32> my question is, how can I keep the rest of the program
    kernal32> running whilst the loop continues in pararel

    A simple way is to insert a check into your main loop:

    my $next_time = time + 5;
    while (1) {
    if (time > $next_time) {
    ... do your once every five seconds thing here ..
    $next_time = time + 5;
    }
    ... rest of code ...
    }

    If other things are event-driven, consider POE, found in the CPAN,
    and described at [url]http://poe.perl.org/[/url].

    print "Just another Perl hacker,"

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz Guest

  8. #7

    Default Re: looping every x seconds

    kernal32 wrote:
    > In PERL how do I execute a block of code every "x" seconds?
    > I'm talking a loop or similar but it runs every x seconds, where x is
    > given.
    That is not possible from Perl.
    You can put your the Perl program to rest for x seconds using sleep(). But
    this is different from running the loop every x seconds.
    Just imagine it takes y seconds to run the loop. Then the next start of the
    loop will happen x+y seconds after the previous start.

    If you really want/need to run the code every x seconds then you may need to
    set up an external timer (depending on your needs for precision could be
    another program or a system task or a hardware timer) which sends a signal
    to your program and thus triggers the loop.

    jue


    Jürgen Exner Guest

  9. #8

    Default Re: looping every x seconds

    Jürgen Exner <jurgenex@hotmail.com> wrote in comp.lang.perl.misc:
    > kernal32 wrote:
    > > In PERL how do I execute a block of code every "x" seconds?
    > > I'm talking a loop or similar but it runs every x seconds, where x is
    > > given.
    >
    > That is not possible from Perl.
    Why on earth not?
    > You can put your the Perl program to rest for x seconds using sleep(). But
    > this is different from running the loop every x seconds.
    > Just imagine it takes y seconds to run the loop. Then the next start of the
    > loop will happen x+y seconds after the previous start.
    What's stopping you from looking how long it took and subtracting that
    from the sleep time? Or something equivalent, more robust? Within
    the precision of the timer, it is quite possible to run a Perl loop
    at specified intervals.

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139