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!
Related
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.
I need to start an asynchronous method call in PHP but have no idea or clue in how to achieve this. The big idea is as follow:
public myfunctionAction() {
// normal flow
// execute the asynchronous call (WS)
// continue normal flow
}
How I do that? Can any provide a simple example just to use as a start point? It's possible to leave the asynchronous call executing on the background and continue the normal flow?
I'm using Symfony 2.6.x as development framework if that helps a little
1 - You can use curl in php with a low time out
2 - You can use popen to start a batch or script shell without waiting the response : like pclose(popen("ws_start.sh"));
Hope that helps :)
I'm creating a app that requires me to run a second php script while the first script is still running.
I'm new to php programing so I'm sure there's a simple function I can use that I'm just not aware of.
Looking forward to any help...
Shane
Since you are new to PHP I'm guessing you're looking for the include/require (and include_once/require_once) language constructs which will execute another PHP script as if it is part of the current script.
Otherwise if you want it to run as a separate process look into exec, shell_exec, or backticks. If you need the other PHP script to run as a background process make sure to redirect stdout somewhere (a file or maybe /dev/null if you don't need it) so that your currently executing script doesn't have to wait for it to finish to continue executing.
This will actually require us to use some Javascript for an ajax call to execute our PHP and return it's data.
I prefer Jquery, which will look similar to this:
function callPHP(){
$.post('./filetocall.php', {variableid: 'id'}, function (response) {
$("#div_for_return_data").val(response);
});
}
filetocall.php can look like anything. It's output will populate the #div_for_return_data
eg:
<?php echo $_GET['variableid']; ?>
Then just call the Jquery function from anywhere.
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 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.