usleep() php5 使用 40% 的空闲 CPU

发布于 2024-08-26 16:59:24 字数 286 浏览 5 评论 0原文

大家好,我有一个奇怪的问题,

我有一个在 Centos 5.x 上运行的 cli php 脚本,它使用 usleep (有时 1 秒,有时 2 秒,有时 100 毫秒,视情况而定),如果需要一些等待,但我注意到它有一次在 usleep() 上,它似乎使用了大约 40% 的空闲 CPU:

Cpu(s):  5.3%us, 21.3%sy,  0.0%ni, 57.2%id,  0.0%wa,  0.0%hi,  0.0%si, 16.1%st

有什么想法吗?
干杯

Hi guys I have a weird question,

I have a cli php script runs on Centos 5.x which uses usleep (somtimes 1sec, sometimes 2sec, somtimes 100ms it depends) if there is some wait required, but what I have noticed its that once on usleep() it seems to use about 40% of idle CPU:

Cpu(s):  5.3%us, 21.3%sy,  0.0%ni, 57.2%id,  0.0%wa,  0.0%hi,  0.0%si, 16.1%st

any ideas ?
cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

绾颜 2024-09-02 16:59:24

对于一个非常简单的测试用例,这不会发生在我身上。在您的系统上尝试以下操作,看看是否仍然出现过多的 CPU 时间。

脚本 test.php:

<?php
for ($n=0;$n<1000;$n++)
{
  usleep(10);
}
?>

然后在命令行运行: time php test.php

我的结果如下:

[ar@arctic ~]$ cat /etc/redhat-release 
CentOS release 5.2 (Final)
[ar@arctic ~]$ time php test.php 

real    0m1.020s
user    0m0.013s
sys     0m0.006s

您可以看到用户和系统时间与真实时间相比非常非常小(或者已过去)时间。即CPU利用率非常低。

This doesn't happen for me with a very simple testcase. Try the following on your system to see if you still get the excessive CPU time.

Script test.php:

<?php
for ($n=0;$n<1000;$n++)
{
  usleep(10);
}
?>

Then at the command line run: time php test.php

My results are as follows:

[ar@arctic ~]$ cat /etc/redhat-release 
CentOS release 5.2 (Final)
[ar@arctic ~]$ time php test.php 

real    0m1.020s
user    0m0.013s
sys     0m0.006s

You can see that the user and sys time are very very small in comparison to the real (or elapsed) time. i.e. CPU utilisation was very low.

注定孤独终老 2024-09-02 16:59:24

在 Windows 系统下,如果您没有将脚本执行的时间限制设置为 0 (set_time_limit(0);),则 php 可执行文件在使用 sleep 时将消耗多达 50% 的 CPU 资源或 usleep 函数。

Under Windows systems, if you do not set the time limit of the execution of your script to 0 (set_time_limit(0);), the php executable will eat as much as 50% CPU power when using sleep or usleep functions.

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