|
下午发了一帖,没人回复我,这心里这个着急啊,没办法,没人鸟你你就需要自己解决,经过代码层层调试,发现出错的原因是因为我在pcntl_fork后在子进程中仍然用原来的连接资源调用mysql,导致出现MySQL server has gone away,经过google之后给出的解决办法是在pcntl_fork之后强制重新链接一下mysql,下面是官方贴出的例子
PHP复制代码
<?php
[/font][/backcolor][/color][color=#ff800]// Create the MySQL connection
[/font][/backcolor][/color][color=#00bb]$db = mysql_connect($server, $username, $password);
[/font][/backcolor][/color][color=#00bb]$pid = pcntl_fork();
if ( [/font][/backcolor][/color][color=#00bb]$pid == -1 ) {
[/font][/backcolor][/color][color=#ff800]// Fork failed
[/font][/backcolor][/color][color=#0770]exit(1);
} else if ( [/font][/backcolor][/color][color=#00bb]$pid ) {
[/font][/backcolor][/color][color=#ff800]// 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
[/font][/backcolor][/color][color=#00bb]$db = mysql_connect($server, $username, $password, true);
} else {
[/font][/backcolor][/color][color=#ff800]// We are the child
// Do something with the inherited connection here
// It will get closed upon exit
[/font][/backcolor][/color][color=#0770]exit(0);
[/font][/backcolor][/color][color=#00bb]?>
复制代码
在下面是我的代码
在controler的__constroct中已经加载model了
PHP复制代码
<?php
class Cron extends CI_Controller {
public function __construct () {
parent ::__construct ();
$this->load->model('m_monitor', 'model');
}
public function test () {
// 从model中取出的数据
// $data = $this->model->getweb();
$data = array(
0 => array(
'm_name' => 'baidu',
'mid' => '7',
'm_url' => 'http://www.baidu.com'
),
1 => array(
'm_name' => 'sina',
'mid' => '8',
'm_url' => 'http://www.sina.com.cn'
)
);
foreach ($data as $key => $value) {
$pid = pcntl_fork ();
if ($pid == -1) {
exit('子进程创建失败');
} elseif ($pid) {
//pcntl_wait($status);
// model中的db方法返回的是$this->db;
// $this->model->db()->reconnect(true);
$dsn = 'dbdriver://rootlocalhost/webmon';
$this->load->database($dsn);
} else {
//当前时间戳
$time = time();
// //上次任务运行结束时间[从model中查询数据库得到的数据]
$lasttime = $this->model->getruntime(1, $value['mid']);
// //用户设置的任务运行周期[从model中查询数据库得到的数据]
$cron_time = $this->model->gettasktime(1, $value['m_url']);
}
}
}
}
?>
复制代码
运行后发现仍然提示MySQL server has gone away,难道CI 强制重新链接mysql不是我写的那样,求大大们给出解决方法(目前猜测是CI 强制重新链接mysql有问题,或者是我写的不对)
|
评分
-
查看全部评分
|