RE: [PHP] flushing AJAX scripts

2009-03-28 Thread Andrea Giammarchi

As I said if the client abort or close the connection, PHP does not finish its 
execution, it simply exits from the script.

If you run this code and you close the browser before 10 seconds the file will 
never be created, for example.

?php
function write($stuff){
echo$stuff, PHP_EOL.br /, ob_get_clean();
@ob_flush();
@flush();
};
$seconds = 10;
ob_start();
while($seconds){
write('I should do something in '.($seconds--).' seconds');
sleep(1);
}
$fp = fopen('test.txt', 'wb');
fwrite($fp, gmdate('Y-m-d H:i:s'));
fclose($fp);
write('Finished!');
?

but, obviously, if you launch a query via PHP and PHP is killed, MySQL will go 
on performing the query until it has been fully executed but if PHP would like 
to do something else after that query and again, the client leave the page, PHP 
will never perform that operation.

So, unless you are not creating a flagged report in one shoot and your system 
is clever enough to understand what's going on in a secure way, I still suggest 
delayed, crontabbed or extra threads for this task which does not suite/scale 
in a webpage.
In my company we are using multi threading tasks with real status able to 
constantly monitor the situation but we are in C# over SQLServer.
I did something similar in PHP and Ajax and MySQL in the old company but trust 
me, it is not simple if you want to be consistent.

Regards.



 Date: Sat, 28 Mar 2009 00:45:27 -0400
 From: jbw2...@earthlink.net
 To: an_...@hotmail.com
 Subject: Re: RE: [PHP] flushing AJAX scripts
 
 Well, the point was that I had tried the first way, submitting one AJAX 
 request and waiting for it to finish and it was timing out, probably on 
 my firewall which it shouldn't have - but it did. The reports can take 
 10 seconds or 10 minutes to create. Doing it this way I can still load 
 the report even if the original request shuts down after 3-4 minutes.
 
 Jim
 
 Andrea Giammarchi wrote:
  Sorry Jim, I meant Jim when I wrote Kim ... and 
  Phico: 
  http://webreflection.blogspot.com/2008/04/phomet-changes-name-so-welcome-phico.html
 
  Regards
 

  From: an_...@hotmail.com
  To: php-general@lists.php.net
  Date: Fri, 27 Mar 2009 15:55:28 +0100
  Subject: RE: [PHP] flushing AJAX scripts
 
 
  Sorry, Kim, but why on earth you are polling with a second request to know 
  when the first one has finished?
  I mean, when the first request inserts data in the database that's it, 
  you'll manage the end of the request.
 
  $A ---  do stuff; do stuff; do stuff; report ready;
  $B --- report ready?
  $B --- report ready?
  $B --- report ready?
  $B --- report ready?
  report ready; --- notification to A
  $B --- report ready;
 
  the report ready, if it is when $A request has been finished, will be in 
  $A, the polling via $B is absolutely useless, imo.
 
  There is no timeout from Ajax, it simply keep waiting, but obviously if 
  your PHP has max_execution_time 30 seconds and the script execution takes 
  more than 30 seconds there's no polling that could save you.
 
  The same if the user closes the browser, connection lost, bye bye response.
 
  To have a notice, you need Comet, try out Phico but still, a page that 
  requires that much is not suitable for the web. Report creation should be 
  a cronjob in a separed thread if it is that stressful.
 
  Regards
 
  
  Date: Fri, 27 Mar 2009 10:47:10 -0400
  From: jbw2...@earthlink.net
  To: an_...@hotmail.com
  CC: php-general@lists.php.net
  Subject: Re: RE: [PHP] flushing AJAX scripts
 
  My page submits the AJAX request to complete a report that takes some 
  time, and upon completion stores results in a database. A second AJAX 
  request polls every 5 seconds and queries the database if the report is 
  ready. This hopefully will get around any timeout problems I am having 
  with a long running request, and seems to be working. It looks like I 
  can accept the default behavior for now. I don't depend on getting a 
  response from the original request, but is there a point where the AJAX 
  response script will be stopped either by Apache or PHP before it can 
  insert into the database?
 
  Jim

  _
  News, entertainment and everything you care about at Live.com. Get it now!
  http://www.live.com/getstarted.aspx
  
 
  _
  Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.
 
  http://www.microsoft.com/windows/windowslive/products/photos.aspx

 
 
 -- 
 James (Jim) B. White
 tel: (919)-380-9615
 homepage: http://jimserver.net/ 
 

_
More than messages–check out the rest of the Windows Live™.
http://www.microsoft.com/windows/windowslive/

RE: [PHP] flushing AJAX scripts

2009-03-27 Thread Andrea Giammarchi

Some browser would like to receive at list N characters (bytes) even if
you force the flush, before the browser will show those characters.
In
any case, the Ajax request will not be completed until its readyState
will be 4, which means the page execution on the server has finished
(released, php has gone, flush or not flush)
For a task like this one you have few options:
 1 - launch  new thread if your host is able to do it
 2 - use a Comet like response (for php I wrote Phico some while ago)
In any case, I hope this stressful operation cannot be performed from thousand 
of users or you can say bye bye to the service.
Alternatives:
 1 - optimize your database
 2 - delegate the job once a time rather than every click (cronjob)
 3 - if the bottleneck is PHP, create an extension in C to perform the same task

Hope this help.

Regards

P.S.
Internet Explorer a part, you can read the responseText on readystate 3
which will be called different time (most likely for each flush).
If IE is not your target, you could consider this opportunity to read the sent 
stream so far.

 Date: Fri, 27 Mar 2009 08:49:35 +1100
 From: dmag...@gmail.com
 To: jbw2...@earthlink.net
 CC: php-general@lists.php.net
 Subject: Re: [PHP] flushing AJAX scripts
 
 jim white wrote:
  I am using jQuery AJAX request to run a script that can take several 
  minutes to create a report. I want to start the script and immediately 
  echo a response to close the connection and then let the script complete 
  a report which I can get later. I have tried several thing such as
  
  ob_start();
  echo json_encode(array(time=$now, message=Report has started 
  running!));
  ob_end_flush();
 
 Try something like this
 
 echo something;
 flush();
 
 without the ob* stuff.
 
 -- 
 Postgresql  php tutorials
 http://www.designmagick.com/
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

_
Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.

http://www.microsoft.com/windows/windowslive/products/photos.aspx

Re: RE: [PHP] flushing AJAX scripts

2009-03-27 Thread jim white
My page submits the AJAX request to complete a report that takes some 
time, and upon completion stores results in a database. A second AJAX 
request polls every 5 seconds and queries the database if the report is 
ready. This hopefully will get around any timeout problems I am having 
with a long running request, and seems to be working. It looks like I 
can accept the default behavior for now. I don't depend on getting a 
response from the original request, but is there a point where the AJAX 
response script will be stopped either by Apache or PHP before it can 
insert into the database?


Jim


Andrea Giammarchi wrote:

Some browser would like to receive at list N characters (bytes) even if
you force the flush, before the browser will show those characters.
In
any case, the Ajax request will not be completed until its readyState
will be 4, which means the page execution on the server has finished
(released, php has gone, flush or not flush)
For a task like this one you have few options:
 1 - launch  new thread if your host is able to do it
 2 - use a Comet like response (for php I wrote Phico some while ago)
In any case, I hope this stressful operation cannot be performed from thousand 
of users or you can say bye bye to the service.
Alternatives:
 1 - optimize your database
 2 - delegate the job once a time rather than every click (cronjob)
 3 - if the bottleneck is PHP, create an extension in C to perform the same task

Hope this help.

Regards

P.S.
Internet Explorer a part, you can read the responseText on readystate 3
which will be called different time (most likely for each flush).
If IE is not your target, you could consider this opportunity to read the sent 
stream so far.

  

Date: Fri, 27 Mar 2009 08:49:35 +1100
From: dmag...@gmail.com
To: jbw2...@earthlink.net
CC: php-general@lists.php.net
Subject: Re: [PHP] flushing AJAX scripts

jim white wrote:

I am using jQuery AJAX request to run a script that can take several 
minutes to create a report. I want to start the script and immediately 
echo a response to close the connection and then let the script complete 
a report which I can get later. I have tried several thing such as


ob_start();
echo json_encode(array(time=$now, message=Report has started 
running!));

ob_end_flush();
  

Try something like this

echo something;
flush();

without the ob* stuff.

--
Postgresql  php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




_
Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.

http://www.microsoft.com/windows/windowslive/products/photos.aspx
  



--
James (Jim) B. White
tel: (919)-380-9615
homepage: http://jimserver.net/ 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] flushing AJAX scripts

2009-03-27 Thread Andrea Giammarchi

Sorry, Kim, but why on earth you are polling with a second request to know when 
the first one has finished?
I mean, when the first request inserts data in the database that's it, you'll 
manage the end of the request.

$A ---  do stuff; do stuff; do stuff; report ready;
$B --- report ready?
$B --- report ready?
$B --- report ready?
$B --- report ready?
report ready; --- notification to A
$B --- report ready;

the report ready, if it is when $A request has been finished, will be in $A, 
the polling via $B is absolutely useless, imo.

There is no timeout from Ajax, it simply keep waiting, but obviously if your 
PHP has max_execution_time 30 seconds and the script execution takes more than 
30 seconds there's no polling that could save you.

The same if the user closes the browser, connection lost, bye bye response.

To have a notice, you need Comet, try out Phico but still, a page that requires 
that much is not suitable for the web. Report creation should be a cronjob in a 
separed thread if it is that stressful.

Regards

 Date: Fri, 27 Mar 2009 10:47:10 -0400
 From: jbw2...@earthlink.net
 To: an_...@hotmail.com
 CC: php-general@lists.php.net
 Subject: Re: RE: [PHP] flushing AJAX scripts
 
 My page submits the AJAX request to complete a report that takes some 
 time, and upon completion stores results in a database. A second AJAX 
 request polls every 5 seconds and queries the database if the report is 
 ready. This hopefully will get around any timeout problems I am having 
 with a long running request, and seems to be working. It looks like I 
 can accept the default behavior for now. I don't depend on getting a 
 response from the original request, but is there a point where the AJAX 
 response script will be stopped either by Apache or PHP before it can 
 insert into the database?
 
 Jim

_
News, entertainment and everything you care about at Live.com. Get it now!
http://www.live.com/getstarted.aspx

RE: [PHP] flushing AJAX scripts

2009-03-27 Thread Andrea Giammarchi

Sorry Jim, I meant Jim when I wrote Kim ... and 
Phico: 
http://webreflection.blogspot.com/2008/04/phomet-changes-name-so-welcome-phico.html

Regards

 From: an_...@hotmail.com
 To: php-general@lists.php.net
 Date: Fri, 27 Mar 2009 15:55:28 +0100
 Subject: RE: [PHP] flushing AJAX scripts
 
 
 Sorry, Kim, but why on earth you are polling with a second request to know 
 when the first one has finished?
 I mean, when the first request inserts data in the database that's it, you'll 
 manage the end of the request.
 
 $A ---  do stuff; do stuff; do stuff; report ready;
 $B --- report ready?
 $B --- report ready?
 $B --- report ready?
 $B --- report ready?
 report ready; --- notification to A
 $B --- report ready;
 
 the report ready, if it is when $A request has been finished, will be in $A, 
 the polling via $B is absolutely useless, imo.
 
 There is no timeout from Ajax, it simply keep waiting, but obviously if your 
 PHP has max_execution_time 30 seconds and the script execution takes more 
 than 30 seconds there's no polling that could save you.
 
 The same if the user closes the browser, connection lost, bye bye response.
 
 To have a notice, you need Comet, try out Phico but still, a page that 
 requires that much is not suitable for the web. Report creation should be a 
 cronjob in a separed thread if it is that stressful.
 
 Regards
 
  Date: Fri, 27 Mar 2009 10:47:10 -0400
  From: jbw2...@earthlink.net
  To: an_...@hotmail.com
  CC: php-general@lists.php.net
  Subject: Re: RE: [PHP] flushing AJAX scripts
  
  My page submits the AJAX request to complete a report that takes some 
  time, and upon completion stores results in a database. A second AJAX 
  request polls every 5 seconds and queries the database if the report is 
  ready. This hopefully will get around any timeout problems I am having 
  with a long running request, and seems to be working. It looks like I 
  can accept the default behavior for now. I don't depend on getting a 
  response from the original request, but is there a point where the AJAX 
  response script will be stopped either by Apache or PHP before it can 
  insert into the database?
  
  Jim
 
 _
 News, entertainment and everything you care about at Live.com. Get it now!
 http://www.live.com/getstarted.aspx

_
Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.

http://www.microsoft.com/windows/windowslive/products/photos.aspx

Re: RE: [PHP] flushing AJAX scripts

2009-03-27 Thread jim white
Well, the point was that I had tried the first way, submitting one AJAX 
request and waiting for it to finish and it was timing out, probably on 
my firewall which it shouldn't have - but it did. The reports can take 
10 seconds or 10 minutes to create. Doing it this way I can still load 
the report even if the original request shuts down after 3-4 minutes.


Jim

Andrea Giammarchi wrote:
Sorry Jim, I meant Jim when I wrote Kim ... and 
Phico: http://webreflection.blogspot.com/2008/04/phomet-changes-name-so-welcome-phico.html


Regards

  

From: an_...@hotmail.com
To: php-general@lists.php.net
Date: Fri, 27 Mar 2009 15:55:28 +0100
Subject: RE: [PHP] flushing AJAX scripts


Sorry, Kim, but why on earth you are polling with a second request to know when 
the first one has finished?
I mean, when the first request inserts data in the database that's it, you'll 
manage the end of the request.

$A ---  do stuff; do stuff; do stuff; report ready;
$B --- report ready?
$B --- report ready?
$B --- report ready?
$B --- report ready?
report ready; --- notification to A
$B --- report ready;

the report ready, if it is when $A request has been finished, will be in $A, 
the polling via $B is absolutely useless, imo.

There is no timeout from Ajax, it simply keep waiting, but obviously if your 
PHP has max_execution_time 30 seconds and the script execution takes more than 
30 seconds there's no polling that could save you.

The same if the user closes the browser, connection lost, bye bye response.

To have a notice, you need Comet, try out Phico but still, a page that requires 
that much is not suitable for the web. Report creation should be a cronjob in a 
separed thread if it is that stressful.

Regards



Date: Fri, 27 Mar 2009 10:47:10 -0400
From: jbw2...@earthlink.net
To: an_...@hotmail.com
CC: php-general@lists.php.net
Subject: Re: RE: [PHP] flushing AJAX scripts

My page submits the AJAX request to complete a report that takes some 
time, and upon completion stores results in a database. A second AJAX 
request polls every 5 seconds and queries the database if the report is 
ready. This hopefully will get around any timeout problems I am having 
with a long running request, and seems to be working. It looks like I 
can accept the default behavior for now. I don't depend on getting a 
response from the original request, but is there a point where the AJAX 
response script will be stopped either by Apache or PHP before it can 
insert into the database?


Jim
  

_
News, entertainment and everything you care about at Live.com. Get it now!
http://www.live.com/getstarted.aspx



_
Drag n’ drop—Get easy photo sharing with Windows Live™ Photos.

http://www.microsoft.com/windows/windowslive/products/photos.aspx
  



--
James (Jim) B. White
tel: (919)-380-9615
homepage: http://jimserver.net/ 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] flushing AJAX scripts

2009-03-26 Thread Chris

jim white wrote:
I am using jQuery AJAX request to run a script that can take several 
minutes to create a report. I want to start the script and immediately 
echo a response to close the connection and then let the script complete 
a report which I can get later. I have tried several thing such as


ob_start();
echo json_encode(array(time=$now, message=Report has started 
running!));

ob_end_flush();


Try something like this

echo something;
flush();

without the ob* stuff.

--
Postgresql  php tutorials
http://www.designmagick.com/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] flushing AJAX scripts

2009-03-26 Thread Raymond Irving

Hello Jim,

You will need to do some low level ajax coding from the client side with the 
onreadychange event. If you're using jquery 1.3 then you can create your own 
XHR object with the xhr callback handler.

To learn more about the ready state check out this link:
http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_2.html

You might find what you're looking for with the following ready state:

3 - Interactive Downloading, responseText holds the partial data. 


Raymond Irving
Create Rich PHP Web Apps Today!
Raxan PDI - http://raxanpdi.com/

--- On Thu, 3/26/09, jim white jbw2...@earthlink.net wrote:

 From: jim white jbw2...@earthlink.net
 Subject: [PHP] flushing AJAX scripts
 To: php-general@lists.php.net
 Date: Thursday, March 26, 2009, 4:01 PM
 I am using jQuery AJAX request to run
 a script that can take several minutes to create a report. I
 want to start the script and immediately echo a response to
 close the connection and then let the script complete a
 report which I can get later. I have tried several thing
 such as
 
 ob_start();
 echo json_encode(array(time=$now,
 message=Report has started running!));
 ob_end_flush();
 
 However, the script does not respond and (I suppose close
 the connection) until the report is complete. How can I fix
 this behaviour?
 
 Jim
 
 
 -- James (Jim) B. White
 tel: (919)-380-9615
 homepage: http://jimserver.net/ 
 
 -- PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php