如何让 php 对 CPU 更友好?

发布于 2024-07-09 21:24:34 字数 187 浏览 14 评论 0原文

我正在共享 *NIX 服务器(由 Site5 运行)上运行。 我有一个在后台运行的 php 脚本,偶尔会进行一些离线计算。 它在运行时使用大约 100% 的 CPU。 我已经尝试过很好地对待它,就像这样:

nice -n 19 php script.php

但这似乎没有任何区别。

I'm running on a shared *NIX server (run by Site5). I have a php script that runs in the background occasionally doing some offline calculations. It uses around 100% CPU while it runs. I've tried nice-ing it, like this:

nice -n 19 php script.php

but that doesn't seem to make any difference.

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

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

发布评论

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

评论(5

甚是思念 2024-07-16 21:24:34

您可以通过代码分散 usleep ( int $micro_seconds )。 这将迫使您的脚本停止一小段时间,从而使 CPU 可以用于其他事情。

但这有必要吗? 如果您的脚本优先级较低,那么它使用 100% 的 CPU 是否重要...如果其他具有较高优先级的进程需要 CPU,它们不会获得所需的时间,而您的脚本会获得其余的时间(最多100%)?

You could scatter usleep ( int $micro_seconds ) through your code. This will force your script to stop for tiny amounts of time leaving the CPU free for other things.

Will that be necessary though? If you script has a low priority, does it matter that it's using 100% of the CPU... If other processes with a higher priority needed the CPU wouldn't they get the time they needed and your script getting the rest (up to 100%)?

奢华的一滴泪 2024-07-16 21:24:34

即使很好,它也会使用 100% CPU(如果可用)。 但是,内核将优先考虑出现的任何其他(非 niced)进程。

Even niced, it'll use 100% CPU if available. However, the kernel will give priority to any other (non-niced) processes that come along.

仄言 2024-07-16 21:24:34

只要系统响应良好,并且您能够在其运行时完成其他工作,我就不会担心。 我在系统上运行分布式计算客户端,它会占用所有可用的 CPU 周期。 由于它以最低优先级运行,因此任何和所有其他进程都会根据需要抢占它。

As long as the system is responsive, and you are able to get other work done while it's running, I wouldn't worry about it. I run a distributed computing client on my systems, and it soaks up any available CPU cycles. Since it runs with the lowest priority, any and all other processes will preempt it as needed.

最丧也最甜 2024-07-16 21:24:34

由于您的进程是后台进程,并且使用 100% CPU,因此该进程似乎受 cpu 限制。 它是后台,因此不会出现用户限制,因此唯一的选择是 IO 限制。 如果您的进程不应该真正执行有趣的 IO,则脚本本身将受到 CPU 限制,而不仅仅是有错误。

流程总是会尝试尽可能快地进行。 如果它们是 IO 密集型的,它们将使用 100% IO,如果它们是 CPU 密集型的,它们将尝试使用 100% CPU。 正确编写的进程调度程序自动旨在为所有进程提供公平感,这意味着较大的进程获得较低的优先级。 您可以使用nice进一步降低优先级。 事实上,CPU 使用率仍然约为 100%,这意味着当前没有其他进程受 CPU 限制,但很可能正在等待来自网络的输入。

As your process is background, and uses 100% CPU it seem that the process is cpu bound. It is background so user bound would not be expected, so the only alternative would be IO bound. If your process should not really do interesting IO, the script itself would be expected to be CPU bound, and not just buggy.

Processes will always try to go as fast as possible. If they are IO bound, they will use 100% IO, if they are CPU bound, they will try to use 100% CPU. Properly written process schedulers automatically aim to provide a sense of fairness to all processes, meaning that the bigger processes get lower priority. You can further lower the priority with nice. The fact that the cpu usage is still about 100% means that there are no other processes which are currently CPU bound, but are most likely waiting for input from the network.

毁梦 2024-07-16 21:24:34

如果有相当多的进程需要上下文切换,Nice 会更改 php 导致 100% CPU 的频率,而不是多少。

当程序执行系统调用时,内核可以在执行实际工作之前将 CPU 交给另一个进程。 你的 php 在全速运行时似乎没有进行任何系统调用。 也就是说,您没有进行任何阻塞 I/O(套接字、文件等)。查看是否有任何代码块陷入忙等待状态。

Nice changes how often your php results in 100% CPU, if there are considerable processes to context-switch, not how much.

The moment a program executes a system call, the kernel can yield the CPU to another process before doing the real job. Your php doesn't seem to make any system call when it's running full speed. That is, you're not making any blocking I/Os (socket, file, etc.) See if any code block is stuck in busy-waiting.

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