Laravel Artisan Queues-高CPU使用率 [英] Laravel Artisan Queues - high cpu usage

查看:144
本文介绍了Laravel Artisan Queues-高CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Laravel中为我的处理脚本设置了队列. 我正在使用beantalkd和主管. 有6种不同的管用于不同类型的处理.

问题在于,对于每根管子,工匠每秒都在不断产卵工人. 工作程序代码似乎休眠了1秒钟,然后工作程序线程使用7-15%的cpu,将其乘以6根管...我希望每根管有多名工作人员.我的cpu被吃光了. >

我尝试将1秒钟的睡眠时间更改为10秒. 这有帮助,但是当工作人员醒来时,每10秒仍然会有大量的CPU高峰. 此时我什至没有处理任何事情,因为队列完全是空的,只是工人在寻找要做的事情.

我还测试了当我在浏览器中刷新页面时laravel的cpu使用情况,它徘徊在10%左右..我现在在低端机架空间实例上,以便可以对其进行解释,但仍然...似乎工人每次醒来时都会启动一个laravel实例.

有没有办法解决这个问题?我是否只需要向一台更昂贵的服务器中投入大量资金,以便能够聆听看工作是否准备就绪?

找到了解决方案...那就不要使用工匠的queue:listener或queue:work 我调查了队列代码,似乎没有办法解决此问题,每次工作人员检查更多工作时,都需要laravel加载.

相反,我使用pheanstalk编写了自己的侦听器. 我仍在使用laravel将内容推送到队列中,然后我的自定义侦听器将解析队列数据,然后触发artisan命令运行.

现在我的监听器的CPU使用率在%0以下,我的CPU现在唯一一次出现故障的时间是它实际找到要执行的工作然后触发命令时,我对此表示满意.

解决方案

CPU高的问题是由于工作程序每次检查队列中的作业时都加载了完整的框架.在laravel 4.2中,您可以使用php artisan queue:work --daemon.这将一次加载框架,并且作业的检查/处理在while循环内进行,这使CPU轻松工作.您可以在官方文档中找到有关守护程序工作程序的更多信息: http://laravel.com/docs/queues#daemon-队列工作者.

但是,这种好处有一个缺点-部署代码时需要特别注意,并且必须照顾数据库连接.通常,长时间运行的数据库连接会断开连接.

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.

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.

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.

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?

EDIT:

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.

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.

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.

解决方案

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 Queues-高CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆