example of how to use fastcgi_finish_request() - php

Can someone show a simple example on how to use fastcgi_finish_request() function?
I googled but only found some general mention of it, some people say they use it successfully but I could not find a single example with code.
For example, I have a PHP object. To send a response to a browser I generate HTML, then
returning it via getResult(). Then echo the result.
Like this:
$obj = new controller();
echo $o->getResult();
Let's say I want to take advantage of this optimization technique to send result to browser and then finish up some potentially long process like connecting to some API, like maybe Facebook API.
How would I go about doing this? I understand that basically I can call fastcgi_finish_request(); and then continue executing php script.
I just need to see example code, I'm not smart enough to figure it out by myself.

I understand that basically I can call fastcgi_finish_request(); and then continue executing PHP script.
Yes, that's all you have to do.
$obj = new controller();
echo $o->getResult();
fastcgi_finish_request();
do_facebook_thing();
To convince yourself it is working, do this:
echo "Test";
fastcgi_finish_request();
sleep(10);
If you remove the second line, then you will see that the browser has to wait 10 seconds.

Related

PHP shell_exec: Php allways wait for finish [duplicate]

Can someone show a simple example on how to use fastcgi_finish_request() function?
I googled but only found some general mention of it, some people say they use it successfully but I could not find a single example with code.
For example, I have a PHP object. To send a response to a browser I generate HTML, then
returning it via getResult(). Then echo the result.
Like this:
$obj = new controller();
echo $o->getResult();
Let's say I want to take advantage of this optimization technique to send result to browser and then finish up some potentially long process like connecting to some API, like maybe Facebook API.
How would I go about doing this? I understand that basically I can call fastcgi_finish_request(); and then continue executing php script.
I just need to see example code, I'm not smart enough to figure it out by myself.
I understand that basically I can call fastcgi_finish_request(); and then continue executing PHP script.
Yes, that's all you have to do.
$obj = new controller();
echo $o->getResult();
fastcgi_finish_request();
do_facebook_thing();
To convince yourself it is working, do this:
echo "Test";
fastcgi_finish_request();
sleep(10);
If you remove the second line, then you will see that the browser has to wait 10 seconds.

Does ajax have to wait for a PHP script to finish before displaying the output

I'm using ajax to run a simple PHP script which echos numbers. To test if ajax would display each echo as it happens or if it waits for the whole script to finish before displaying anything.
<?php
echo '1';
sleep(5);
echo '2';
sleep(5);
echo '3';
sleep(5);
echo '4';
sleep(5);
echo '5';
?>
This has shown me that it does wait for the whole script to finish before displaying the numbers.
I would like to know if it is possible to output each echo as it happens, similar to how you would see it in shell?
No that's impossible as the PHP script must run and return it's output before it is supplied to the AJAX request.
No it is not possible to wait for fractial result. But you can use websockets to achieve this behaviour.
For example you can use php library Ratchet or use javascript (or other language) as server.
There are many different ways, techniques and software on how to implement what you need.
Here is a basic and easy example for you: https://github.com/panique/php-long-polling
It is based on a long polling technique.
But I would suggest you to use a centrifu-go with sockJs library for a large projects.

PHP GTK refreshing GUI

I've been trying to make a very simple PHP application using php-gtk. The program does some processing and outputs the status of that process. The problem is that the application doesn't launch until the process is finished.
I read that the line while (Gtk::events_pending()) {Gtk::main_iteration();} allows the main loop to continue while processing but it doesn't work for me.
Here's the code:
<?php
if(!class_exists('gtk')){
exit('php-gtk2!!');
}
$wnd = new GtkWindow();
$wnd->set_size_request(400, 200);
$wnd->set_title('test');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
$lbl = new GtkLabel('1/3');
function processing($lbl){
while (Gtk::events_pending()) {Gtk::main_iteration();}
sleep(2);
$lbl->set_text('2/3');
sleep(2);
$lbl->set_text('3/3');
}
processing($lbl);
$wnd->add($lbl);
$wnd->show_all();
Gtk::main();
?>
I tried placing that line everywhere on the code and I'm not sure why it doesn't work.
Any help would be really appreciated. Thank you in advance!
(Note: the sleep function is only to simulate some heavy processing)
Since you are trying to do work at the same time as your GUI is running, you will need to use a second thread, communicating from that thread to the GUI thread to get GUI updates. To do this, use the gdk_threads_add_idle() or g_idle_add() functions. Do not call GTK+ functions directly from the other thread!

I want to execute more than one method at a time in php

Hi Please help me in executing more than one method at a time in PHP.
Below is example:
<?php
function writeName()
{
sleep(3);
echo "Kai Jim Refsnes";
}
function b(){
sleep(3);
echo"b";
}
b();
writeName());
?>
Here above program take 6 sec to execute.But I want to run my both method simultaneously so that program should execute with in 3 sec(Multi threading).
With common PHP its not possible, because PHP is executed sequential. You may have a look at a job-server like gearman, or you may try to use forks (pcntl_fork()). It's not multi-threading, because there is no shared memory.
Sorry, but multithreading is not supported in PHP.
But you could start a PHP script which can run in the background using exec(). Just make sure you redirect it's output elsewhere.
That should be the closest you can get to "multithreading" without additional tools. Here's what the manual says:
Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

Can PHP tell when the browser goes away?

If I'm generating a stream of data to send out to a browser, and the user closes the browser, can I tell within PHP that I don't need to bother generating or sending the rest of the stream? I'd like to insert something into this loop:
while (!feof($pipes[1])) {
echo fgets($pipes[1]);
}
My fallback plan is to have the browser use a JavaScript onunload to hit another PHP page to kill the process that's generating the data, but it would be cleaner if PHP could tell when I'm echoing to nowhere.
By default PHP will abort the script if the user navigates away. There are however times where you don't want this to happen so php has a config you set called ignore_user_abort.
http://php.net/manual/en/misc.configuration.php
There's also a function called register_shutdown_function() that is supposedly executed when execution halts. I've never actually used it, so I won't vouch for how well it works, but I thought I'd mention it for completeness.
I believe that script will automatically abort when loaded normally (No ajax). But if you want to implement some sort of long polling via php using xmlhttprequest I think you will have to do it with some sort of javascript because then php can't detect it. Also like to know the precise case.
These answers pointed me towards what I was looking for. The underlying process needed special attention to kill it. I needed to jump out of the loop. Thanks again, Stack Overflow.
while (!feof($pipes[1]) && !connection_aborted())
{
echo fgets($pipes[1]);
}
if (connection_aborted())
{
exec('kill -4 '.$mypid);
}

Categories