并行Cron任务期间的MySQL连接关闭 [英] MySQL Connection Closing During Parallel Cron Tasks
问题描述
我已经基于这两个博客文章为基于cron服务的并行任务编写了一个基于Zend框架的cron服务:
I have written a Zend Framework based cron service for parallel tasks based on these two blog articles:
- Cron tasks in Zend Framework apps
- Zend Framework Cron Tasks in Parallel
总而言之,cron服务使用 pcntl_fork()
并行。
In summary, the cron services uses pcntl_fork()
to spawn the tasks in parallel.
使用服务运行单个任务无问题,但当我添加第二个任务时,我收到此MySQL错误:
Running a single task with the service works without issues, but when I add a second task, I get this MySQL error:
一般错误:2006 MySQL伺服器已消失
General error: 2006 MySQL server has gone away
子线程在另一个之前结束,并且MySQL连接被隐式关闭。如果是这样,我如何确保连接保持打开,直到父线程关闭?
My best guess is that a child thread ends before the other and the MySQL connection is implicitly closed. If this is the case, how do I ensure that the connection stays open until the parent thread closes?
推荐答案
在 pcntl_fork()
和此问题,这确实是孩子共享父连接的问题。我已经添加了这段代码,以创建一个新的MySQL连接后分叉,它似乎已经解决了问题:
After reading the comments on pcntl_fork()
and this SO question, it was indeed the issue with children sharing the parent connection. I have added this code to create a new MySQL connection after forking, and it seems to have fixed the problem:
// give this thread its own db connection
$settings = Zend_Registry::get('settings');
$db = Zend_Db::factory(
$settings->db_adapter,
array(
'host' => $settings->db_host,
'username' => $settings->db_user,
'password' => $settings->db_pass,
'dbname' => $settings->db_name,
)
);
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($db);
这篇关于并行Cron任务期间的MySQL连接关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!