注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

php pcntl_fork多线程  

2011-04-13 15:26:37|  分类: php |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

来源:php手册

The reason for the MySQL "Lost Connection during query" issue when forking is the fact that the child process inherits the parent's database connection. When the child exits, the connection is closed. If the parent is performing a query at this very moment, it is doing it on an already closed connection, hence the error.

An easy way to avoid this is to create a new database connection in parent immediately after forking. Don't forget to force a new connection by passing true in the 4th argument of mysql_connect():

<?
// Create the MySQL connection
$db = mysql_connect($server, $username, $password
);

$pid = pcntl_fork
();
            
if (
$pid == -1
) {       
  
// Fork failed           
  
exit(1
);
} else if (
$pid
) {
  
// We are the parent
   // Can no longer use $db because it will be closed by the child
   // Instead, make a new MySQL connection for ourselves to work with
  
$db = mysql_connect($server, $username, $password, true
);
} else {
  
// We are the child
   // Do something with the inherited connection here
   // It will get closed upon exit
  
exit(0
);
?>

This way, the child will inherit the old connection, will work on it and will close upon exit. The parent won't care, because it will open a new connection for itself immediately after forking.

Hope this helps.
 
 
 
 
 
 
 
 
I think this simple code can help understand how fork works:

<?
echo "posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n"
;

$pid = pcntl_fork
();
if (
$pid == -1) die("could not fork"
);
if (
$pid
) {
   echo
"pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n"
;
} else {
   echo
"pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n"
;
}
?>
 
 
 
 
 
 
 
 
When using php 4.3.3, I found that output buffering can really confuse you if you use fork within your script.

The buffer (everything which hasn't been placed on the screen yet) is copied to the child process.

This means that when the child flushes it's buffer, which happens automatically when the script ends, you will get a copy of everything which your code (before the fork) tried to place on to the screen.

This causes it to look like all your code before the fork has run twice, when actually it hasn't.

This doesn't happen on my other server running a newer version of php, but that might be a configuration issue.

Note that using ob_implicit_flush() at the start of the script doesn't appear to solve the problem. Adding ob_end_flush() before the pcntl_fork() command does solve the problem.

Example:
<?
echo "This is an echo before I called the fork command\n"
;

// uncomment the next line to fix the described problem
#ob_end_flush(); 

$pid = pcntl_fork
();
if (
$pid == -1
) {
   die(
"could not fork"
);
} else if (
$pid
) {
   echo
"I am the parent, pid = ". $pid ."\n"
;
} else {
   echo
"I am the child, pid = ". $pid ."\n"
;
}
?>

Output, before "fixed":

This is an echo before I called the fork command
I am the parent, pid = 15449
This is an echo before I called the fork command
I am the child, pid = 0

Output, after "fixed":

This is an echo before I called the fork command
I am the parent, pid = 15023
I am the child, pid = 0
 
 
 
 
 
 
 
 
I was writing a shell script to get input from a user, however, I needed my script to time out after a certain number of seconds if the user didn't enter enough data. The code below descibes the method I used. It's a little hairy but it does work.

-Ben

#!/home/ben/php/bin/php -q
<?
//GLOBALS
$RETURN_CHAR = "\n"
;
$TIMEOUT = 5;
//number of seconds to timeout on input
$PID = getmypid
();
$CHILD_PID = 0
;

//Make sure program execution doesn't time out
set_time_limit(0
);

function
set_timeout
() {
global
$PID
;
global
$CHILD_PID
;
global
$TIMEOUT
;

$CHILD_PID = pcntl_fork
();
if(
$CHILD_PID == 0
) {
sleep($TIMEOUT
);
posix_kill($PID, SIGTERM
);
exit;
}
}

function
clear_timeout
() {
global
$CHILD_PID
;
posix_kill($CHILD_PID, SIGTERM
);
}

// read_data()
// gets a line of data from STDIN and returns it
function read_data
() {

$in = fopen("php://stdin", "r"
);
set_timeout
();
$in_string = fgets($in, 255
);
clear_timeout
();
fclose($in
);
return
$in_string
;
}

// write_data($outstring)
// writes data to STDOUT
function write_data($outstring
) {
$out = fopen("php://stdout", "w"
);
fwrite($out, $outstring
);
fclose($out
);
}

while(
1
) {
write_data("say something->"
);
$input = read_data
();
write_data($RETURN_CHAR.$input
);
}

?>
 
  评论这张
 
阅读(2097)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017