Laravel Artisan 队列 - 高 CPU 使用率 [英] Laravel Artisan Queues - high cpu usage
问题描述
我在 Laravel 中为我的处理脚本设置了队列.我正在使用 beanstalkd 和 supervisord.有 6 种不同的管用于不同类型的处理.
I have set up queues in Laravel for my processing scripts. I am using beanstalkd and supervisord. There are 6 different tubes for different types of processing.
问题在于,对于每根管子,工匠每秒都在不断地产生工人.工作代码似乎休眠了 1 秒,然后工作线程使用 7-15% 的 cpu,将其乘以 6 个管......我希望每管有多个工作......我的 cpu 被吃光了.
The issue is that for each tube, artisan is constantly spawning workers every second. The worker code seems to sleep for 1 second and then the worker thread uses 7-15% cpu, multiply this by 6 tubes... and I would like to have multiple workers per tube.. my cpu is being eaten up.
我尝试将 1 秒睡眠更改为 10 秒.这有帮助,但是当工作人员醒来时,每 10 秒仍然会有一个巨大的 CPU 峰值.我现在什至什么都没有处理,因为队列完全是空的,只是工人在找事情做.
I tried changing the 1 second sleep to 10 seconds. This helps but there is still a huge cpu spike every 10 seconds when the workers wake back up. I am not even processing anything at this time because the queues are completely empty, it is simply the workers looking for something to do.
当我在浏览器中刷新页面时,我还测试了 laravel 的 cpu 使用率,它徘徊在 10% 左右.我现在在一个低端机架空间实例上,这样可以解释它,但仍然......似乎工作人员每次醒来都会启动一个 laravel 实例.
I also tested to see the cpu usage of laravel when I refreshed the page in a brower and that was hovering around 10%.. I am on a low end rackspace instance right now so that could explain it but still... it seems like the workers spin up a laravel instance every time they wake up.
有没有办法解决这个问题?我是否只需要在更昂贵的服务器上投入大量资金就可以监听工作是否准备就绪?
Is there no way to solve this? Do I just have to put a lot of money into a more expensive server just to be able to listen to see if a job is ready?
找到了解决方案...不使用工匠队列:侦听器或队列:工作我查看了队列代码,似乎没有办法解决这个问题,每次工作人员检查是否有更多工作要做时,它都需要加载 laravel.
Found a solution... it was to NOT use the artisan queue:listener or queue:work I looked into the queue code and there doesn't seem to be a way around this issue, it requires laravel to load every time a worker checks for more work to do.
相反,我使用 pheanstalk 编写了自己的监听器.我仍然使用 laravel 将东西推入队列,然后我的自定义侦听器正在解析队列数据,然后触发 artisan 命令运行.
Instead I wrote my own listener using pheanstalk. I am still using laravel to push things into the queue, then my custom listener is parsing the queue data and then triggering an artisan command to run.
现在我的侦听器的 cpu 使用率低于 %0,我的 cpu 现在唯一一次爆发是当它真正找到工作要做然后触发命令时,我很好.
Now my cpu usage for my listeners is under %0, the only time my cpu shoots up now is when it actually finds work to do and then triggers the command, I am fine with that.
推荐答案
高CPU问题是因为worker每次检查队列中的作业时都会加载完整的框架.在 laravel 4.2 中,你可以使用 php artisan queue:work --daemon
.这将加载一次框架,并在 while
循环中检查/处理作业,这让 CPU 可以轻松呼吸.你可以在官方文档中找到更多关于 daemon worker 的信息:http://laravel.com/docs/queues#daemon-队列工作者.
The problem of high CPU is caused because the worker loads the complete framework everytime it checks for a job in the queue. In laravel 4.2, you can use php artisan queue:work --daemon
. This will load the framework once and the checking/processing of jobs happen inside a while
loop, which lets CPU breathe easy. You can find more about daemon worker in the official documentation: http://laravel.com/docs/queues#daemon-queue-worker.
但是,这种好处也有一个缺点 - 部署代码时需要特别小心,并且必须注意数据库连接.通常,长时间运行的数据库连接会断开.
However, this benefit comes with a drawback - you need special care when deploying the code and you have to take care of the database connections. Usually, long running database connections are disconnected.
这篇关于Laravel Artisan 队列 - 高 CPU 使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!