Page 1 of 2
How to run second php script under the main script
Posted: Tue Sep 09, 2008 12:42 pm
by girishkoshti007
Hi,
I have 2 php files, i want one php file(a.php) to execute / run the other php file (b.php) but not wait for that file (b.php) to complete its execution for it to complete its (a.php) own execution as well. is this possible , if yes then how?
thanks in advance.
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 12:51 pm
by Darkzaelus
That would require multithreading within php, which, as far as I know, does not exist. Sorry. PHP would run through file 1, hit the include/require, complete b.php, then complete a.php under the include/require statement.
Cheers, Darkzaelus
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 1:01 pm
by panic!
Why the hell would you want to do that haha? maybe we can think of a better way of doing this..
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 1:03 pm
by marcth
This is a very interesting problem you raise. Although, I've never heard of multi-threading in PHP, I'm sure it'd be possible to emulate it. I did a quick search on the net and came up with this post--looks like a winner to me:
Pseudo Multithreaded PHP.
Please post back with your results--I'm curious.
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 1:06 pm
by dude81
why not use some ajax code from a.php to do
Code: Select all
xmlhttp.open("GET", "b.php",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==2) {
alert("b.php responded, we will wait until state 4");
}
}
xmlhttp.send(null)
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 1:13 pm
by panic!
You couldn't guarantee they run at the same time that way though.
I can't see why anyone would want two scripts to run at the same time..why not use the code from both in one?
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 2:31 pm
by pickle
panic! wrote:I can't see why anyone would want two scripts to run at the same time..why not use the code from both in one?
You could have a situation where, after a user account is created, you have 1001 servers that need to be updated to reflect the new account. If you've got a user signing up for your cool new social networking site, you don't want them to have to wait for 1001 servers to update. You'd want to initiate the process, then carry on with the rest of your work.
~girishkoshti007: I've never seen a way to do what you're asking. Depending on what you want b.php to do though, it might be possible to use cron & not execute b.php in real-time, but at short, regular intervals.
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 3:11 pm
by marcth
panic! wrote:You couldn't guarantee they run at the same time that way though.
I can't see why anyone would want two scripts to run at the same time..why not use the code from both in one?
I have no idea why he wants to do this. In all my 10+ years of PHP programming, I've never encountered this situation--which is why, to me, the question is very interesting.
In my mind I came up with this abstract scenario: Task A needs to be processed (35 secs), Task B needs to be processed (30 secs) and Task C processes takes Task A with Task B to produce the final result (2 secs).
Traditionally, this would take 1 minutes and 7 seconds to process. If said solution worked, it'd take 37 seconds (plus or minus a bit) to process.
? + that = profit!
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 3:25 pm
by VladSun
You can pass the arguments to a php CLI and put the process in background like this:
Code: Select all
$runCommand = "php -q /path/to/php/you/want/to/execute/in/background param1 param2";
if(isset($_SERVER['PWD'])//*nix (aka NOT windows)
{
$nullResult = `$runCommand > /dev/null &`;
}
else //windowz
{
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($runCommand, 7, false);
}
PS: from
viewtopic.php?f=1&t=72617&p=411214
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 5:06 pm
by pickle
Doesn't that still pause PHP's execution until the shell command is finished?
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 5:21 pm
by panic!
can't you just set a cron job? or make a fake cron?
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 5:25 pm
by panic!
pickle wrote:Doesn't that still pause PHP's execution until the shell command is finished?
Yes it does
pickle wrote:
You could have a situation where, after a user account is created, you have 1001 servers that need to be updated to reflect the new account. If you've got a user signing up for your cool new social networking site, you don't want them to have to wait for 1001 servers to update. You'd want to initiate the process, then carry on with the rest of your work.
Well you let your load-balancing deal with that!

Most load-balancing solutions are fairly transparent these days. You definitely wouldn't be dealing with this sort of problem in the 'controller' stage of an application anyway!
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 5:57 pm
by pickle
Not all applications have a 'controller' stage. MVC isn't perfect for every purpose.
Whether my example is something that would usually happen or not, it's a valid example of why the original poster might have this situation.
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 6:38 pm
by marcth
panic! wrote:can't you just set a cron job? or make a fake cron?
Yes. But that's not the pont!

Edit: You don't ever need to fake a cron job.
Re: How to run second php script under the main script
Posted: Tue Sep 09, 2008 9:02 pm
by VladSun
pickle wrote:Doesn't that still pause PHP's execution until the shell command is finished?
Yes, it does

It's insane - even pcntl_fork() doesn't help ... I suppose it has something to do with Apache.