cgi bg

2014-08-25 Thread Erez D
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.

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 ?


thanks
erez
___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread Erez D
On Mon, Aug 25, 2014 at 10:29 AM, Jonathan Ben Avraham y...@tkos.co.il
wrote:

 Hi Erez,
 Did you include the response header

 Connection: close

 ?

yes


  - yba


 On Mon, 25 Aug 2014, Erez D wrote:

  Date: Mon, 25 Aug 2014 10:25:49 +0300
 From: Erez D erez0...@gmail.com
 To: linux-il linux-il@cs.huji.ac.il
 Subject: cgi bg


 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.

 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 ?


 thanks
 erez



 --
  9590 8E58 D30D 1660 C349  673D B205 4FC4 B8F5 B7F9  ~. .~  Tk Open Systems
 =} Jonathan Ben-Avraham (yba) --ooO--U--Ooo-
 ---{=
 mailto:y...@tkos.co.il tel:+972.52.486.3386 http://tkos.co.il
 skype:benavrhm
 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread Jonathan Ben Avraham

Hi Erez,
Did you include the response header

Connection: close

?

 - yba


On Mon, 25 Aug 2014, Erez D wrote:


Date: Mon, 25 Aug 2014 10:25:49 +0300
From: Erez D erez0...@gmail.com
To: linux-il linux-il@cs.huji.ac.il
Subject: cgi bg

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.

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 ?


thanks
erez




--
 9590 8E58 D30D 1660 C349  673D B205 4FC4 B8F5 B7F9  ~. .~  Tk Open Systems
=} Jonathan Ben-Avraham (yba) --ooO--U--Ooo{=
mailto:y...@tkos.co.il tel:+972.52.486.3386 http://tkos.co.il skype:benavrhm___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread shimi
On Mon, Aug 25, 2014 at 10:25 AM, Erez D erez0...@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
___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread Eli Billauer

Hi,

As far as I recall, the connection closes only when the cgi script's 
stdout closes, which is usually when the script terminates.


If you fork() the process or just go on doing stuff, a file handle 
attached to stdout remains open, which is probably why the HTTP 
connection remains open.


I suggest trying to close stdout when you're done with the HTTP response.

Regards,
   Eli

On 25/08/14 10:25, Erez D 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.


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 ?



thanks
erez


___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
   



___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread Erez D
thanks,


not so easy to use, as i can not use stdout anymore
but it works.


On Mon, Aug 25, 2014 at 10:57 AM, shimi linux...@shimi.net wrote:

 On Mon, Aug 25, 2014 at 10:25 AM, Erez D erez0...@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


___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: cgi bg

2014-08-25 Thread guy keren


you can re-open stdout and point it to a file (perhaps even to /dev/null).

On 08/25/2014 11:41 AM, Erez D wrote:

thanks,


not so easy to use, as i can not use stdout anymore
but it works.


On Mon, Aug 25, 2014 at 10:57 AM, shimi linux...@shimi.net
mailto:linux...@shimi.net wrote:

On Mon, Aug 25, 2014 at 10:25 AM, Erez D erez0...@gmail.com
mailto:erez0...@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




___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il




___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il