Page 1 of 1
exit without flushing
Posted: Tue Aug 17, 2010 10:18 am
by shawngoldw
I have a script and with certain conditions I call exit(). How can I set the script up so that the output does not get flushed to the user when the script terminates?
A simple solution would be to use ob_start() before print and then clear it before exiting. This will not work for me. I have stuff echo'd and print'd and I need to clear something on or before exit.
Thank you,
Shawn
Re: exit without flushing
Posted: Tue Aug 17, 2010 10:27 am
by AbraCadaver
You'll have to be more specific about why ob_start() and ob_end_clean() won't work.
Re: exit without flushing
Posted: Tue Aug 17, 2010 10:30 am
by pickle
If you have stuff output before your script runs, then there's nothing you can do to stop it. If your script generates absolutely all output, then there's no reason why ob_start() and ob_end_clean() shouldn't work.
Re: exit without flushing
Posted: Tue Aug 17, 2010 10:45 am
by shawngoldw
They will work, but they will make everything much more complicated to write.
This is a problem arising from a parallel processing script I wrote,
Code: Select all
class Railway
{
// pids of running processes
var $pids;
// shm references
var $shm;
// pointers to return variables
var $returns;
// dispatch a new thread
function dispatch($file, &$params = null, &$return = null)
{
// start new thread
$pid = pcntl_fork();
if ($pid == -1) // fork failed
{
return false;
}
$this->returns[$pid] =& $return;
$this->_start_shm($pid);
if ($pid > 0) // parent
{
return true;
}
else // child
{
// include requested file
include($file);
exit;
}
}
// private
function _start_shm($pid)
{
$this->pids[] = $pid;
if ($pid == 0)
{
$pid = getmypid();
}
$this->shm[$pid] = shm_attach($pid);
}
// only for use by children
function set_return($value)
{
shm_put_var(end($this->shm), 1, $value);
}
// wait for all threads to finish
function wait(&$status = null)
{
//wait for each thread to finish
for ($i=0; $i < count($this->pids); $i++)
{
pcntl_wait($status);
}
foreach ($this->pids as $key => $value)
{
$shm =& $this->shm[$value];
if (isset($this->returns[$value]))
{
if (shm_has_var($shm, 1))
{
$this->returns[$value] = shm_get_var($shm, 1);
}
}
shm_remove($shm);
shm_detach($shm);
}
$this->pids = null;
$this->shm = null;
$this->returns = null;
}
}
I'm using pcntl_fork and with pcntl_fork the child process get the output of the parent, so when the child finishes, the output in the parent prints in the child too. echo's and print's are really just being used for debugging purposes, actual content gets printed after the parallel processing so there is not a problem with the actual content.
Talking stuff through helps, I may just be able to flush before forking. That may work.
edit: yup, flush() before pcntl_fork() did the trick. Waste of a thread, thanks anyways