How to run second php script under the main script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

girishkoshti007
Forum Newbie
Posts: 5
Joined: Tue Sep 09, 2008 12:13 pm

How to run second php script under the main script

Post 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.
Darkzaelus
Forum Commoner
Posts: 94
Joined: Tue Sep 09, 2008 7:02 am

Re: How to run second php script under the main script

Post 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
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to run second php script under the main script

Post by panic! »

Why the hell would you want to do that haha? maybe we can think of a better way of doing this..
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: How to run second php script under the main script

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: How to run second php script under the main script

Post 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)
 
 
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to run second php script under the main script

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to run second php script under the main script

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: How to run second php script under the main script

Post 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!
Last edited by marcth on Tue Sep 09, 2008 6:45 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to run second php script under the main script

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to run second php script under the main script

Post by pickle »

Doesn't that still pause PHP's execution until the shell command is finished?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to run second php script under the main script

Post by panic! »

can't you just set a cron job? or make a fake cron?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: How to run second php script under the main script

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to run second php script under the main script

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: How to run second php script under the main script

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to run second php script under the main script

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply