How do I stop php from returning headers when I pcntl_fork()?
It seems that my php prints X-Powered-By and Content-Type headers for every child made by pcntl_fork(). this wouldn't be a problem if it didn't print them in the middle of the output.
So, for instance, this toy script:
function very_long_process($shm){
sleep(20);
shm_put_var($shm,0,'terminated');
}
function iterate_until_terminated($shm){
$signal = shm_get_var($shm,0);
if($signal=='running'){
$j = shm_get_var($shm,1);
$j++;
shm_put_var($shm,1,$j);
sleep(2);
iterate_until_terminated($shm);
}
else{
exit;
}
}
$shm = shm_attach(ftok(tempnam('/tmp','PHP'),'a'),1000000);
shm_put_var($shm,0,'running');
$i=0;
shm_put_var($shm,1,$i);
$pid = pcntl_fork();
if($pid==0){
iterate_until_terminated($shm);
}
very_long_process($shm);
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
}
$iterated = shm_get_var($shm,1);
$signal = shm_get_var($shm,0);
echo "<p>iterated $iterated times.";
echo "<p>process was $signal";
Produces the output (in a browser):
X-Powered-By: PHP/5.2.17
Content-type: text/html
<p>iterated 10 times.<p>process was terminated
I've read, and would like to believe, that the solution is to use ob_start() and ob_end_clean(), but I've tried that in a couple places and it didn't work, although (weirdly) with ob_end_flush() I was able to keep the number of extra header pairs to two. So uh.. where to buffer output?
Of course, I'd be just as happy with a solution that didn't involve output buffering.
Thanks for your help!
Edit: This particular toy fork is meant to simulate passing a request to a model, reading its output stream of dat开发者_C百科a with a view, and then translating that via a templating engine to be picked up by a periodic ajax request.
I know there are probably lots of things wrong with that, but I don't want to get off-topic here. Is there a way to suppress the headers?
I'm pretty sure apache is adding those headers, not php. That is why output buffering is not working. You'll have to get those turned off in apache.
精彩评论