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

Hello, I have a need to push an HTTP request to a script on a different server, but I do not want to wait for a return. I am using LWP (with get()) to make the call currently, but it is adding unnecessary time to the generation of my page. The purpose of the script is to update the expiration date on a session. I am not sure what to search on to find the right routines. Any help would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: server to server communication
by McDarren (Abbot) on Jun 15, 2006 at 02:14 UTC
    Use the fork, young Luke ;)
      Thanks for the suggestion Luke. I have read the documentation for fork and it will spawn a UNIX process. From there I have to have UNIX call the other server process? I may be making too much of this, but my understanding is I would have to set up a local perl script that redirects to the HTTP address I am interested in and have the fork process call that local script?

      What would be ideal is:
      &get("http://www.mysite.com", do_not_wait_for_return);
      I know get() does not take those kind of parameters, I am hoping some other function within perl does.

      Thanks.
        Yes, I think you are making too much of this :)

        Before I go on, I'll just say from the outset that I've not written a lot of code that uses forking, so I won't profess to be an expert on the subject.

        However, based on my understanding, here is a simple example to demonstrate how fork works.

        #!/usr/bin/perl -wl use strict; defined (my $pid = fork) or die "Cannot fork: $!"; unless ($pid) { for (1 .. 20) { print "CHILD: $_"; sleep 1; } exit 0; } for (1 .. 20) { print "PARENT: $_"; sleep 1; }

        When the above is executed, you get both loops printing at the same time. That is, the second loop doesn't wait for the first to complete. Which is pretty much what you are after, no?

        Cheers,
        Darren :)

Re: server to server communication
by jdhedden (Deacon) on Jun 15, 2006 at 17:43 UTC
    You can also try to use threads:
    threads->create(sub { get($url); })->detach();
    This puts the call the 'get' in the 'background', and allows your main thread to continue working.

    Remember: There's always one more bug.