How to run second php script under the main script
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
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.
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
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
Cheers, Darkzaelus
Re: How to run second php script under the main script
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
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.
Please post back with your results--I'm curious.
Re: How to run second php script under the main script
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
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 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
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.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?
~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.
Re: How to run second php script under the main script
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.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?
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.
Re: How to run second php script under the main script
You can pass the arguments to a php CLI and put the process in background like this:
PS: from viewtopic.php?f=1&t=72617&p=411214
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);
}
There are 10 types of people in this world, those who understand binary and those who don't
Re: How to run second php script under the main script
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.
Re: How to run second php script under the main script
can't you just set a cron job? or make a fake cron?
Re: How to run second php script under the main script
Yes it doespickle wrote:Doesn't that still pause PHP's execution until the shell command is finished?
Well you let your load-balancing deal with that!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.
Re: How to run second php script under the main script
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.
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.
Re: How to run second php script under the main script
Yes. But that's not the pont!panic! wrote:can't you just set a cron job? or make a fake cron?
Re: How to run second php script under the main script
Yes, it doespickle wrote:Doesn't that still pause PHP's execution until the shell command is finished?
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