mysql 查询并发_MySQL可以支持每个连接的并发查询吗?

declare(ticks=1);

pcntl_signal(SIGUSR1, create_function('$signo', 'sleep(1);while (($pid=pcntl_wait(@$status, WNOHANG))>0) {}'));//protect against zombie children

$pdo=new PDO('mysql:host=192.168.0.2;port=3306;dbname=baseinfo', 'dev', 'dev',

array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,

PDO::MYSQL_ATTR_INIT_COMMAND=>'set names utf8'

)

);

for ($i=0; $i<20; ++$i)

{if (($pid=pcntl_fork())===-1)

{//...

continue;

}

else if ($pid)

{$pids[]=$pid;

pcntl_wait($status, WNOHANG); //protect against zombie children, one wait vs one child

}

else if ($pid===0)

{ob_start();//prevent output to main process

register_shutdown_function(create_function('$pars', 'ob_end_clean();posix_kill(posix_getppid(), SIGUSR1);posix_kill(getmypid(), SIGKILL);'), array());//to kill self before exit();, or else the resource shared with parent will be closed

for ($j=0; $j<200; ++$j)

{try

{file_put_contents('log.'.$i.'.txt', $pdo->query('select partner_login from base_account where id=100')->fetch(PDO::FETCH_COLUMN, 0)."t".time().substr(microtime(),2,6)."n", FILE_APPEND);

}

catch (Exception $e)

{if ($pdo->getAttribute(PDO::ATTR_SERVER_INFO)==='MySQL server has gone away')

{file_put_contents('log.conn.txt', time().substr(microtime(),2,6).":{$i}:{$j} lostn", FILE_APPEND);

$pdo=&new PDO('mysql:host=192.168.0.2;port=3306;dbname=baseinfo', 'dev', 'dev',

array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,

PDO::MYSQL_ATTR_INIT_COMMAND=>'set names utf8'

)

);

}

}

usleep(50000);

}

exit();//avoid foreach loop in child process

}

}

//wait all child to end, avoid close db connection before all children self killed

foreach ($pids as $p)

{pcntl_waitpid($p, $status);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值