For what it's worth, you're not really supposed to design PHP applications this way. It's theoretically meant to be fully synchronous, since it's supposed to handle HTTP requests, which are fully synchronous themselves.
Given your example, if task 1 is a long-running process, you're eventually going to need task 2 to sleep for some period of time, or poll whatever is controlling task 1 to see if it's finished. In doing so, you run the risk of your HTTP request timing out ifyou don't respond. If you're having to do this, it's better to just do the entire business in one PHP process.
A better way (but not the only way) to handle this, assuming task 1 is a sufficiently long running process that the user would notice, might be to do something like this:
//PHP - longrunningprocess.php
$jobId = generateJobId();
//Actually launch the long-running process. There are alternatives to exec, such Icicle:
//https://github.com/icicleio/icicle
exec('/path/to/long_running_script.php arg1 arg2 arg3');
echo '{ "jobId":'.$jobId.'}';
//end
//PHP - longrunningprocessresult.php
$jobId $_GET['jobId'];
var result = new LongRunningProcessResult();
var jobStatus = getJobStatus();
if(jobStatus.complete != true)
{
result.complete = true;
result.property1 = jobStatus.property1;
//...
}
else
{
result.complete = false;
}
echo json_encode(result);
And then on the client side, something like this:
function handleJobSuccess(results)
{
//Do whatever you do with the results
}
function checkForCompletion(jobId)
{
setTimeout(function() {
$.ajax("longrunningprocessresult.php?jobId=" + jobId, {success: function(args){
if(args.success)
{
handleJobSuccess(args);
}
else
{
checkForCompletion(jobId);
}
}
});
}, 5000);
}
function beginLongRunningProcess()
{
showWorkingThrobber();
$.ajax('longrunningprocess.php', { success: function(args){
var jobId = args.jobId;
checkForCompletion(jobId);
}});
}
The important takeaway for this being that you don't want to send a request to the server for a long running process that just ties up the browser while it runs. You need to implement a UI that at least implies to the user that the process is working, and then handles the result asynchronously.