in reply to GET a file without waiting for the result?

My current solution times out because the file takes to long to fetch.
Uhm, if the file takes a long time to fetch, it's going to take a long time to fetch even if you do something else in the mean time. If you get a time out during the fetch, it's unlikely to not time out if you do it "asynchronously".

As for doing it "asynchronously", there are three classical solutions: use an event loop, fork a new process, spawn a new thread. I don't see how any of them will help with a timeout.

  • Comment on Re: GET a file without waiting for the result?

Replies are listed 'Best First'.
Re^2: GET a file without waiting for the result?
by DreamT (Pilgrim) on Sep 22, 2010 at 13:44 UTC
    Hmm. The main problem is that the script is supposed to run via Apache, and is supposed to print "1", which will signal that it's finished. After this it will fetch the file and do some other stuff, but this won't be printed to the user.

    Maybe it's possible to tell Apache that the script is finished immediately after the "1" is printed, so that the web page stops loading, but the script continues to work in the "background"?
    There isn't a problem that the script takes time, the problem is that the system that calls the URL can't wait for the webpage to load.
        Kind of.
        Maybe there's a simpler way?:-D

      It's curious that you don't want any error checking (in which case you could use Watch Long Processes Through CGI).

      I can see two approaches.

      • Spawn a task to do the work.

        use POSIX qw( setsid ); print "Content-Type: text/plain\n"; print "\n"; print "1"; my $pid = fork(); exit(0) if !defined($pid) || $pid; open STDIN, '</dev/null' or die; open STDOUT, '>/dev/null' or die; open STDERR, '>&STDOUT' or die; setsid(); ... handle request ...
      • Assign the work to an existing task or to a cron job.

        ... queue request in a file or database ... print "Content-Type: text/plain\n"; print "\n"; print "1";