并行 Cron 任务期间 MySQL 连接关闭
我根据以下两篇博客文章编写了一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阅读
pcntl_fork()
和此 SO问题,这确实是孩子共享父连接的问题。我添加了这段代码来在分叉后创建一个新的 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: