并行 Cron 任务期间 MySQL 连接关闭

发布于 2024-11-15 01:08:21 字数 572 浏览 0 评论 0原文

我根据以下两篇博客文章编写了一个基于 Zend Framework 的并行任务 cron 服务:

总之, cron服务使用 pcntl_fork() 并行生成任务。

使用该服务运行单个任务不会出现任何问题,但是当我添加第二个任务时,出现以下 MySQL 错误:

一般错误:2006 MySQL 服务器已消失

我最好的猜测是子线程先于另一个子线程结束,并且 MySQL 连接隐式关闭。如果是这种情况,如何确保连接保持打开状态直到父线程关闭?

I have written a Zend Framework based cron service for parallel tasks based on these two blog articles:

In summary, the cron services uses pcntl_fork() to spawn the tasks in parallel.

Running a single task with the service works without issues, but when I add a second task, I get this MySQL error:

General error: 2006 MySQL server has gone away

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情域 2024-11-22 01:08:21

阅读 pcntl_fork() 和此 SO问题,这确实是孩子共享父连接的问题。我添加了这段代码来在分叉后创建一个新的 MySQL 连接,它似乎已经解决了问题:

// 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文