如何在运行 cronjob PHP 脚本时处理 CPU IO 峰值

发布于 2024-11-26 19:06:15 字数 145 浏览 0 评论 0原文

嘿,由于每 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 技术交流群。

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

发布评论

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

评论(2

夜还是长夜 2024-12-03 19:06:15

Usleep

只要一点点 usleep 就会返回 CPU 到其他可用进程(CPU 调度)

Hog

以这个简单的脚本为例:

<?php

for ($i=0;$i<1000000;$i++) {
    echo "$i\n";
}

这个过程平均消耗了我 20% 的 CPU 时间。

计划

这个简单的脚本平均只消耗 10% 的 CPU 时间。

<?php

for ($i=0;$i<1000000;$i++) {
    echo "$i\n";
    usleep(100);
}

当然,这个脚本确实需要更长的时间,但 CPU 得到了更好的调度。你睡眠的时间越长,CPU 的调度能力就越好。例如,usleep(1000) 仅使用了 2% 的 CPU 时间。

我在我的 Ubuntu Box

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 10.10
Release:    10.10
Codename:   maverick

消息队列

上测试了这个,而且你的操作系统非常擅长调度进程(当然该进程需要对你的 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:

<?php

for ($i=0;$i<1000000;$i++) {
    echo "$i\n";
}

This process consumes 20% of my CPU-time on average.

Schedule

This simple script only consumes 10% CPU-time on averqage.

<?php

for ($i=0;$i<1000000;$i++) {
    echo "$i\n";
    usleep(100);
}

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

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 10.10
Release:    10.10
Codename:   maverick

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.

风情万种。 2024-12-03 19:06:15

购买更多 CPU 能力或[u]sleep()n< /code>请求。

您还可以从 sys_getloadavg 并决定您是否需要sleep()(以及需要多少时间)。请记住,睡眠过多可能会导致每个 CRON 花费超过 30 分钟。

Buy more CPU power or [u]sleep() every n requests.

You can also get the CPU load from sys_getloadavg and decide if (and how much) you need to sleep(). Bare in mind that sleeping too much may cause each CRON to take longer than 30 minutes.

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