Optimized redirection (cgi/perl)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HQ
    Confirmed User
    • Jan 2001
    • 3539

    #1

    Optimized redirection (cgi/perl)

    Here's a simple redirection perl script:

    Code:
    #!/usr/bin/perl -wT
    print "Location:[url]http://www.nba.com/\n\n[/url] ";
    sleep(15);
    exit;
    If you run it, you will notice that it waits 15 seconds before going to nba.com, even though nothing else is required to be 'printed' to the browser. The sleep is stuck in there to represent tracker calculations that have to be perform.

    Is there anyway to cause the redirect to take place immediately, and then perform the calculations afterwards?
  • Babaganoosh
    ♥♥♥ Likes Hugs ♥♥♥
    • Nov 2001
    • 15841

    #2
    Not efficiently. The only way I can think of to do something like that is to fork() a process off and do the calculations there, but I absolutely would not do that unless it is absolutely necessary. Hopefully though, your code is smooth enough to do the calculations before the user is forwarded. Simple mathematic calculations should be carried out in a fraction of a second. The user shouldn't get impatient and close the browser in that .10 of a second.
    I like pie.

    Comment

    • mike503
      Confirmed User
      • May 2002
      • 2243

      #3
      uh yeah. you should be able to do all the processing before and then fire off the location header..

      if you're doing that complicated of processding you may need to move to either real-time analysis (not real-time calculation) or go with a batch approach (i.e. cronjob every x minutes.)
      php/mysql guru. hosting, coding, all that jazz.

      Comment

      • NetRodent
        Confirmed User
        • Jan 2002
        • 3985

        #4
        Try using the following code...

        Code:
        #!/usr/bin/perl -wT
        $| = 1;
        print "Location:[url]http://www.nba.com/\n\n[/url] ";
        sleep(15);
        exit;
        The $| variable controls how perl buffers output. Setting
        it to 1 tells it not to buffer anything but output immediately.
        In general its a bad idea not to buffer but this will redirect
        immediately.
        "Every normal man must be tempted, at times, to spit on his hands, hoist the black flag, and begin slitting throats."
        --H.L. Mencken

        Comment

        • [Labret]
          Registered User
          • May 2001
          • 10945

          #5
          You randomization layers are too low.

          Add a couple of glitch targeting mechanisms in order to better bedunktify the layering effect caused by the circumpreferential treatment of African Americans. fo shizzy.

          Comment

          • Phil21
            Confirmed User
            • May 2001
            • 993

            #6
            what netrodent said should work well.

            -Phil
            Quality affordable hosting.

            Comment

            • HQ
              Confirmed User
              • Jan 2001
              • 3539

              #7
              Labert, what?

              NetRodent, cool i'll try that. When the script in question is only going to output a redirect (one line of text), then 'turning off' the buffer should not be a problem. Damn, I never knew there was a buffer, this is why I was confused by the waiting.

              mik503, I do batch processing. I'm creating a tracker which logs raw data and it gets processed every update of the site. However, sending hits out and tracking hits in has to be accomplished in real time. The only calculations are updates to these real-time tables that stores url info, hits in, hits out, clicks produced, and where every IP has gone so I don't send it back to the same place twice. The calculations should be quick, but some of these are calculations that can be performed AFTER the destination is known (for permanent links). For blind links, the calcuations have to be performed beforehand.

              Armed & Hammered, I thought of performing a fork too, but NetRodent's idea is the best solution I think.

              Thanks all.

              Comment

              • HQ
                Confirmed User
                • Jan 2001
                • 3539

                #8
                Originally posted by NetRodent
                The $| variable controls how perl buffers output. Setting
                it to 1 tells it not to buffer anything but output immediately.
                In general its a bad idea not to buffer but this will redirect
                immediately.
                One question. Does the $| variable change the buffer of outputs to files too? As my redirect script will be modifying files (which means it will be outputing their full contents).

                Comment

                • NetRodent
                  Confirmed User
                  • Jan 2002
                  • 3985

                  #9
                  Here's what the perl documentation has to say about the $| variable at http://www.perldoc.com/perl5.6/pod/perlvar.html

                  autoflush HANDLE EXPR
                  $OUTPUT_AUTOFLUSH
                  $|
                  If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See perlfunc/getc for that. (Mnemonic: when you want your pipes to be piping hot.)
                  So to answer your question, setting the $| changes the buffering on all output including files. Of course you can do something like
                  this:

                  Code:
                  #!/usr/bin/perl -wT
                  $| = 1; # unbuffer this
                  print "Location:[url]http://www.nba.com/\n\n[/url] ";
                  $| = 0; # buffer everything else
                  open(LOG, ">>log");
                  print LOG "blah blah blah\n";
                  close LOG;
                  exit;
                  It also appears that you may be able to set the buffering on particular output channels. Try reading the perl IO faqhere.
                  "Every normal man must be tempted, at times, to spit on his hands, hoist the black flag, and begin slitting throats."
                  --H.L. Mencken

                  Comment

                  • HQ
                    Confirmed User
                    • Jan 2001
                    • 3539

                    #10
                    $|=1; made the redirection work faster but it was messing up my file writing (I did a $|=0; right after the redirect). Very strange. No scripting errors or anything, just that the file wasn't being written to where it was before.

                    Comment

                    Working...