Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

A continually running process

by becca23 (Novice)
on Jun 25, 2008 at 20:10 UTC ( [id://694039]=perlquestion: print w/replies, xml ) Need Help??

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

I am currently writing a script that runs continually, but only actually does something every two minutes. I had previously achieved this by doing this:
while() { ...code... sleep(120); }
However, the sleep function has become a terrible labryinth of problems to my script and I would like to find a more efficient way to run my script continually than with a while loop and a sleep function. Any suggestions? Oh and this is on a Win32 platform. This is what the script does essentially. It automates an rsync backup of files betwen two servers every two minutes. The problem I was having with sleep is that it delays any print/output file statements until after the sleep has executed, and this often resulted in output files getting truncated. for example if i did this:
print "hello"; sleep(120);
The script would wait two minutes before printing out hello. Very strange problem and i don't know how to fix it, maybe you guys do?

Replies are listed 'Best First'.
Re: A continually running process
by Fletch (Bishop) on Jun 25, 2008 at 20:54 UTC

    Erm, sleep shouldn't delay anything that occurs before it. More sounds like you're suffering from buffering.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Specifically, have you turned on autoflush?
      $|=1; while ( 1 ) { print "hello"; sleep( 120 ); }

      Lastly may I say how impressed I am that you are somehow getting rsync running from a Win32 platform!

        It was quite a challenge! If you're looking to do it yourself, try Deltacopy, this is what I used. It does a minimal cygwin install on the windows platform--just enough to allow you to run rsync, ssh, and chmod as if you were on a regular linux OS.
Re: A continually running process
by jettero (Monsignor) on Jun 25, 2008 at 20:16 UTC

    I was about to say POE until I read the win32 part. I think it's meant to work under win32, but I'd guess there exist problems with it.

    What don't you like about the while/sleep loop? It seems fine to me really. Are there problems with sleep in win32?

    If nothing else, sleep for 1 or 2 seconds and just check the time with localtime and compare it against a $last_run variable or something like that.

    -Paul

Re: A continually running process
by psini (Deacon) on Jun 25, 2008 at 20:22 UTC

    Well, you should tell us why this solution is problematic, and perhaps something on what the program does.

    One of the many possible alternatives is to set an alarm and a signal handler to trigger an event in an event driven architecture.

    But, really, I can't say if it is a viable solution not knowing anything of your project...

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: A continually running process
by GrandFather (Saint) on Jun 25, 2008 at 21:05 UTC

    Please do not make significant updates to your node content without indicating that the content has been updated. Most people indicate an update using Update:.

    Part of your problem is that you need to turn off buffered output by setting $| to 1. There are likely to be other issues such as writing the affected files from multiple tasks, but those are much harder to solve without knowing a lot more about what you are doing.


    Perl is environmentally friendly - it saves trees
Re: A continually running process
by zentara (Archbishop) on Jun 26, 2008 at 11:35 UTC
    If you can get Glib installed on win32, I would Roll your own Event-loop and use a timer. If you have Tk running with ActiveState, it has timers too. Also see How to implement a timer in perl?.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; $mw->withdraw; $mw->repeat(1000, \&launch); MainLoop; sub launch{ system( "date" ); }

    I'm not really a human, but I play one on earth CandyGram for Mongo

      Getting Glib installed on Win32 just to obtain a timer would be most perverse.

      All windows programs have message loop accessible and therefore, an event loop and timers, along with a whole plethora of other waitable timers and synchronisation mechanisms.

      Suggesting Glib or Tk in order to avoid disabling buffering on STDOUT is, well, let's just say: overkill.


      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.
        Well you are probably right, however my point was to make becca23 aware of the benefit of using timers rather than sleep..... where or how she gets the timer makes no matter to me. Timers alllow program expansion and/or control, which sleep precludes.

        I'm not really a human, but I play one on earth CandyGram for Mongo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found