cgi bg
shimi
linux-il at shimi.net
Mon Aug 25 10:57:12 IDT 2014
On Mon, Aug 25, 2014 at 10:25 AM, Erez D <erez0001 at gmail.com> wrote:
> hi
>
> i have a php cgi scripts that
> 1. generates an http response , this takes less than a second
> 2. do some stuff that may take some time, lets say a minute
>
> when posting to that cgi, although the html is returned in less then a
> second, the request is not closed until the minute has passed.
>
> The request will end when PHP will tell its upstream that it has ended.
After all, it may still produce output, which the client is supposed to
receive.
> i want the http transaction to be closed when done (i.e. less than a
> minute)
> but the php script to continue it's action (e.g. the minute it takes)
>
> can i do it in php ? i.e. flush, or send eof, which will finish the
> request but leave the php running until done ?
>
>
You could at the worst case execute the code from an external file with a
system() and backgrounded (append & to the command), a solution that will
always work (but is ugly).
An alternative approach which was possible in the past was to use
http://php.net/register-shutdown-function to handle the request 'cleanup'
(which is what I assume you are trying to do) - but since PHP 4.1 this
stuff is no longer possible because now this can also send output to the
client. Assuming you have a newer PHP... which is highly likely... you
could try this instead:
<?php
ob_end_clean();
header("Connection: close");
ignore_user_abort(); // optional
ob_start();
echo ('Text the user will see');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush(); // Strange behaviour, will not work
flush(); // Unless both are called !
// Do processing here
sleep(30);
echo('Text user will never see');
?>
( Shamelessly copied from http://php.net/connection-handling )
The idea is to buffer all the response in memory, then measure the buffer
size of the response, then tell that to the server/client, and also let the
connection to not support keep-alive. Then throw everything to the client.
Since the response is of a given size, and the server/client has got all of
it, it has nothing to do further with the server, so it has no reason not
to close the socket.
HTH,
-- Shimi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20140825/65172850/attachment.html>
More information about the Linux-il
mailing list