PHP fork和mysql数据库连接问题

发布于 2024-08-26 05:46:08 字数 861 浏览 0 评论 0原文

我现在正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2024-09-02 05:46:08

尽量不要在父进程中打开另一个SQL连接,并在每个子线程中使用不同的链接标识符变量创建另一个到MySQL的链接。

<?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"; 
      // do nothing with connection here, use old one ($db)
    }
    else
    { 
      $db2 = mysql_connect("server","user","pwd", true); 
      echo "child process $rv\n"; 
      $sql1 = "another query"; 
      $res1 = mysql_query($sql1,$db2); 
      while($messages = mysql_fetch_array($res1)) { 
        $sql2 = "update query"; mysql_query($sql2,$db2); 
      } 
      exit(0); 
    } 
} 
?>

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.

<?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"; 
      // do nothing with connection here, use old one ($db)
    }
    else
    { 
      $db2 = mysql_connect("server","user","pwd", true); 
      echo "child process $rv\n"; 
      $sql1 = "another query"; 
      $res1 = mysql_query($sql1,$db2); 
      while($messages = mysql_fetch_array($res1)) { 
        $sql2 = "update query"; mysql_query($sql2,$db2); 
      } 
      exit(0); 
    } 
} 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文