如何在运行 cronjob PHP 脚本时处理 CPU IO 峰值
嘿,由于每 30 分钟运行一次 PHP 脚本,我遇到了一些 CPU 峰值。 脚本每天都会向注册的 Twitter 用户发送推文,并且有很多用户。 所以基本上当 PHP 脚本发送 twits 时,它会导致 CPU 峰值。
我请求指导我应该如何处理这种情况。多谢。
Hey there I am having some CPU spikes due to PHP script I run every 30 mins.
Script sends twits to signed up twitter users everyday and there are a lot of users.
So basically when PHP script sends out twits it causes a CPU spike.
I am asking for a direction on how should I handle this situation. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Usleep
只要一点点 usleep 就会返回 CPU 到其他可用进程(CPU 调度)。
Hog
以这个简单的脚本为例:
这个过程平均消耗了我 20% 的 CPU 时间。
计划
这个简单的脚本平均只消耗 10% 的 CPU 时间。
当然,这个脚本确实需要更长的时间,但 CPU 得到了更好的调度。你睡眠的时间越长,CPU 的调度能力就越好。例如,
usleep(1000)
仅使用了 2% 的 CPU 时间。我在我的 Ubuntu Box
消息队列
上测试了这个,而且你的操作系统非常擅长调度进程(当然该进程需要对你的 CPU 友好),所以我建议你使用消息队列来加速你的工作(发送推文) )。例如 Redis 也可以用作消息队列或 beanstalkd.运行几个处理工作(发送推文)的工作进程。作为奖励,您不会产生相对昂贵的生成过程的价格。在网络上,使用消息队列可以获得足够多的信息。
Usleep
Just a tiny little usleep will return the CPU to other available process(CPU scheduling).
Hog
Take this simple script for example:
This process consumes 20% of my CPU-time on average.
Schedule
This simple script only consumes 10% CPU-time on averqage.
Of-course this script does take a little longer, but the CPU is better scheduled. The longer you usleep the better the CPU can schedule.
usleep(1000)
for example only used 2% CPU-time.I tested this on my Ubuntu Box
Message Queue
Also your operating system is very good at scheduling processes(of course that process needs to be friendly to your CPU) so I would advice you to use a message queue to speed up your work(sending tweets). For example Redis can also be used as a message queue or beanstalkd. Run a couple of worker processes which process work(sending out tweets). As a bonus you don't incur the price of spawning processes which is relative expensive. On the web there is more than enough information available using message queue.
购买更多 CPU 能力或
[u]
sleep()
每n< /code>
请求。
您还可以从
sys_getloadavg
并决定您是否需要sleep()
(以及需要多少时间)。请记住,睡眠过多可能会导致每个 CRON 花费超过 30 分钟。Buy more CPU power or
[u]
sleep()
everyn
requests.You can also get the CPU load from
sys_getloadavg
and decide if (and how much) you need tosleep()
. Bare in mind that sleeping too much may cause each CRON to take longer than 30 minutes.