PHP fork和mysql数据库连接问题
我现在正在尝试在 php 中进行分叉。 我想在子进程中进行一些查询和更新.. 问题是,每当子进程完成时,它就会关闭连接,这会使其他查询失败。 以下是我的示例代码!
#!/usr/local/bin/php
<?php
set_time_limit(0); # forever program!
$db = mysql_connect("server","user","pwd");
mysql_select_db("schema",$db);
$sql = "query";
$res = mysql_query($sql,$db);
while($rows = mysql_fetch_array($res)) {
$rv = pcntl_fork();
if($rv == -1){
echo "forking failed";
}
elseif($rv){
echo "parent process $rv\n";
$db = mysql_connect("server","user","pwd",true);
mysql_select_db("schema",$db);
}
else{
echo "child process $rv\n";
$sql1 = "another query";
$res1 = mysql_query($sql1,$db);
while($messages = mysql_fetch_array($res1)) {
$sql2 = "update query";
mysql_query($sql2,$db);
}
exit(0);
//it terminates both child process and mysql connection!
}
}
?>
I am now trying to do forking in php.
I would like to do some query and update in child process..
the problem is that whenever a child process finish, it close the connection which makes the other queries fail.
The following is my sample code!!
#!/usr/local/bin/php
<?php
set_time_limit(0); # forever program!
$db = mysql_connect("server","user","pwd");
mysql_select_db("schema",$db);
$sql = "query";
$res = mysql_query($sql,$db);
while($rows = mysql_fetch_array($res)) {
$rv = pcntl_fork();
if($rv == -1){
echo "forking failed";
}
elseif($rv){
echo "parent process $rv\n";
$db = mysql_connect("server","user","pwd",true);
mysql_select_db("schema",$db);
}
else{
echo "child process $rv\n";
$sql1 = "another query";
$res1 = mysql_query($sql1,$db);
while($messages = mysql_fetch_array($res1)) {
$sql2 = "update query";
mysql_query($sql2,$db);
}
exit(0);
//it terminates both child process and mysql connection!
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽量不要在父进程中打开另一个SQL连接,并在每个子线程中使用不同的链接标识符变量创建另一个到MySQL的链接。
Try not to open another SQL connection in parent process, and create another link to MySQL in each child thread with different link identifier variable.