1

I have here an myAction function in some controller. And it has one class instance:

public function myAction() {

    ...
    $myAnalyzer = new Analysis();
    $myAnalyzer->analyze();
    ...
}

Let say this function analyze() takes 10 mins. That means it blocks my.phtml 10 mins, which is unacceptable. What I want is to render my.phtml first and then to show intermediate result from analyze() on my.phtml.

function analyze() {

... 
    foreach($items as $rv) {

        ...
        ...
        // new result should be stored in db here 
    }

}

As far as I know, it's impossible, for there is just one thread in PHP. So I decided to ajax-call from my.phtml to run myAnalyzer instance.

First question: is that right? Can I do it in myAction() without blocking?

OK, now I run myAnalyzer using some script, say worker.php, from my.phtml with the help of javascript or JQuery.

Second question: how can I know when each foreach-loop ends? In other words, how can I let worker.php send some signal (or event) to my.phtml or zend framework. I do NOT want to update my.phtml on a time basis using javascript timer. That's all that I need to know, since intermediate data is supposed to be stored in DB.

Third question: the myAnalyzer muss stop, when user leaves pages. For that I have this code.

window.onbeforeunload = function(e) {

    // killer.php kills myAnalyzer
};

But how can javascript communicate with myAnalyzer? Is there something like process-id? I mean, when worker.php runs myAnalyzer, it registers its process-id in zend framework. And when user leave page, killer.php stops myAnalyzer using this process-id.

I appreciate the help in advance.

user1592714
  • 443
  • 4
  • 11
  • This might help http://stackoverflow.com/questions/3833013/continue-php-execution-after-sending-http-response and this too http://php-fpm.org/wiki/Features#fastcgi_finish_request.28.29 – Keyne Viana Sep 03 '12 at 07:19

1 Answers1

1

First Q.: Yeah, I'm afraid that is correct.

Second Q.: I do not understand what do you mean here. See code example below

foreach($data => $item) {
   ...
}
//code here will be executed only after foreach loop is done. 

Third Q.: Take a look at this page. You can set this to false (But I suppose it is already like that) and send something to client from time to time. Or you can set it to true and check if user is still connected with connection_aborted function. What I mean here is that you can run your worker.php with ajax and configure your request so browser will not disconnect it because of timeout (so connection will be kept while user stay on page). But it will be closed if user leave the page.

EDIT: About second question. There are few options:

1) you may use some shared memory (like memcached, for instance). And call server with another ajax request from time to time. So after each loop is ended - you put some value into memcached and during request you can check that value and build response/update your page based on that value

2) There is such thing like partial response. It is possible to get some piece of response with XMLHTTPRequest, but as I remember - that is not really useful at this moment as it is not supported by many browsers. I do not have any details about this. Never tried to use it, but I know for sure that some browsers allow to process portions of response with XMLHTTPRequest.

3) You can use invisible iframe to call your worker.php instead of XMLHTTPRequest. In this case you can send some piece of where you can put a javascript which will call some function in parrent window and that function will update your page. That is one of Long-polling COMET implementations if you want to get some more information. There are some pitfalls (for instance, you may need to ensure that you are sending some specyfic amount of symbols in response in order to get it executed in some browser), but it is still possible to use (some web browser chats are based on this).

2) and 3) is also good because it will solve your third question problem automatically. At the same time 1) may be simpler, but it will not solve a problem in third question.

One more thing - as you will have long running script you must remember that session may block execution of any other requests (if default file based PHP session is used - this will happen for sure)

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • Thanks a lot for the answer. But what about backward communication? I mean from worker.php to my.phtml? Some part of my.phtml muss be re-rendered (maybe through another ajax). The best scenario is right after each foreach-loop. That was my second question. – user1592714 Sep 03 '12 at 06:48
  • Thanks again! Before I go deep into your answer, I want to know whether there is another solution on zend framework level. In this case my.phtml calls workerAction instead of worker.php. There may be hopefully some kind of possibility .... – user1592714 Sep 03 '12 at 08:20
  • I think nothing stops you from calling workerAction like any regular Zend action. But making separate "thread" with some magic zend function - I do not think so. Just remember that zend is caching output and if you will want to send portions of data in response - it may cause problems. – Viktor S. Sep 03 '12 at 08:27