Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

program with a timer

by Anonymous Monk
on Jan 12, 2001 at 14:58 UTC ( [id://51340]=perlquestion: print w/replies, xml ) Need Help??

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

how can one implement a cgi program which operates at regular intervals ? for eg: how can i retrieve the stock quotes from a web page periodically and form a database?

Replies are listed 'Best First'.
Re: program with a timer
by davorg (Chancellor) on Jan 12, 2001 at 15:03 UTC

    That doesn't sound like a CGI program to me.

    I think that you need a Perl script which uses LWP to grab the data from the web page and puts it in the database using DBI/DBD. You'd then set up a cron job to run your script periodically.

    No CGI there at all.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      An alternative for win xx users:

      $five_mins = 300; while(not $exit_condition) { # Run LWP code here sleep($five_mins); }
        On WINNT you can use their AT command to schedule programs to run. I've used it for automating cron-type jobs and it works reasonably well.

        Simply type  

        at/?
        in your DOS/command window to get more information.

        Mick
Re: program with a timer
by zigster (Hermit) on Jan 12, 2001 at 15:11 UTC
    CGI is specifically a user orientated program, it runs only when the user requests a specific page. I am not sure what you are trying to do. My guess is one of two things:
    • You are trying to suck data from the web to a local database for offline viewing (Quote: Retrieve the stock quotes from a web page and form a database) If you want to do this you have a few alternatives, take a look at HTTP client in perl a recient discussion that describes how to do this. You could also have a look at wget, a stand alone application. You will find wget at freshmeat
    • You are trying to provide a web page that refreshes itself to show the latest information take a look at netscapes page on dynamic documentation here specifically it talks about server push and client pull methods for dynamic web content. The example it gives quote:

      The general idea is that browsers have always been driven by user input. You click on a link or an icon or an image and some data comes to you. As soon as people saw they could do that, they wanted to give a server the ability to push new data down to the browser. (An obvious example is a stock trader who wants to see new quote data every 5 minutes.) Up until now, that hasn't been possible.

    I hope this helps, if it does not then post some more details and I will see if I can help further.
    --

    Zigster
Re: program with a timer
by lzcd (Pilgrim) on Jan 12, 2001 at 15:10 UTC
    In addition to investigating the LWP module from CPAN you also may wish to check out the perl command 'sleep'.

    eg.
    while (1==1) { # Loop forever sleep 60*10; # do nothing for 10 minutes # <- insert LWP web page stuff here -> {

    Update:Fell for the old 'lets switch equality for assigment' trick of 1=1 instead of 1==1.
      while (1=1) {
      Can't modify constant item in scalar assignment at ./test.pl line 1, n +ear "1) "

      Did you mean just

      while (1) {
      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        Thanks for spotting my typo.
        I suppose your variation is just as good but TMTOWTDI stikes again with whats familiar for me.
Re: program with a timer
by sutch (Curate) on Jan 12, 2001 at 23:45 UTC
    An alternative to cron or NT's AT is to use your CGI program to retrieve the stock quotes when they have passed their "freshness" time. I've used this method to minimize requests to external hosts:

    - maintain a database of stock symbols, quotes, and the last time retrieved.
    - on each GGI request for a quote, retrieve the quote from the database. If it is not available or the last time retrieved is older than the max freshness time (say, five minutes), grab the quote from the external host and update the database.

    This will ensure that application supplies the latest quotes (within five minutes) while avoiding unnecessary requests for quotes.

Re: program with a timer
by OzzyOsbourne (Chaplain) on Jan 12, 2001 at 19:31 UTC

    The program would have to be consistently running (as a service maybe?) to execute at various times. Why not use a scheduler in your operating system such as the windows AT command to kick off your program?

    -OzzyOsbourne

Re: program with a timer
by tune (Curate) on Jan 13, 2001 at 03:41 UTC
    I would not recommend using a sleep() command. Why? Because the activity you start between two sleeping can be time consuming (it happens when you are working with web and/or databases), and the total time will not satisfy the 5 minute period. So it will be incorrect.

    When you work with sensitive information you can not let yourself do this

    -- tune

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found