PHP 中是否有更好的方法将 CPU 让给其他进程?
我有一些长时间运行的 CLI PHP 脚本,它们通过 cron 定期运行。我希望它们尽快完成,但不会严重影响其他流程(例如 Web 服务器响应能力)。
目前,我正在运行该脚本
nice -n 19
,并且还尝试插入非常短的 usleep() 调用,例如在我的主循环中插入 50 微秒。但在单核虚拟机上,这并不总是能像我希望的那样快。顺便说一句,我没有使 RAM 饱和,所以没有发生分页。
我读过 usleep() 是一个系统调用,它允许调度程序在需要时比没有任何系统调用更快地为其他进程分配优先级。
我想知道是否有更好的方法在 PHP 中执行此操作。例如不休眠但立即产生优先级的调用。
另外,我知道其他语言比 PHP 更高效,但这是用 Symfony+Doctrine 编写的更大应用程序的一部分。我不想分成多种语言并失去应用程序模型的业务逻辑优势。
I have some long-running CLI PHP scripts that run regularly via cron. I'd like them to complete as quickly as possible but without seriously impacting other processes (such as web server responsiveness).
Currently I am running the script with
nice -n 19
and also have experimented with inserting very short usleep() calls, such as 50 microseconds in my main loop. Still this isn't always yielding as quickly as I'd like on a single-core VM. BTW, I'm not saturating RAM so there's no paging happening.
I've read that usleep() is a system call that will allow the scheduler to assign priority to other processes if needed more quickly than if I didn't have any system calls.
What I'm wondering is if there's a better method of doing this in PHP. Such as a call that doesn't sleep but yields priority right away.
Also, I know other languages are more efficient than PHP, but this is part of a bigger app written in Symfony+Doctrine. I don't want to split into multiple languages and lose the business logic benefits of the app's models.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
nice
不适合您,请考虑为您的内核选择不同的调度程序。这是您的操作系统优先处理进程的问题。这不是您可以在应用程序中轻松解决的问题(无论是用 PHP 还是任何其他语言编写的)。哦,请记住,除非您非常缺乏 CPU 周期,否则
nice
级别不会真正发挥作用。如果您的 CPU 大部分时间都处于空闲状态,那么即使是良好级别为 19 的进程也可以吃掉它想要的所有 CPU 周期。编辑:事实上,在走这条路之前请确保您受到 CPU 限制。如果您受 I/O 限制,那么 CPU 优先级不会产生太大影响。
If
nice
isn't cutting it for you, consider picking a different scheduler for your kernel. This is a problem with your OS prioritizing processes. It isn't something that you can readily solve inside your application (whether it's written in PHP or any other language).Oh and remember,
nice
levels don't really kick in unless you're starving for CPU cycles. If your CPU is mostly idle, even a process with a nice level of 19 is allowed to eat all the CPU cycles it wants.Edit: In fact, make sure you're CPU-bound before going down this path. If you're I/O bound then CPU prioritisation isn't going to have much of an effect.
总的来说Linux是非常保守的。但你可以尝试 schedtool 和 libmlock。后者是将php锁定在ram中以避免swap。您也可以尝试交换性,因为大多数情况下都非常高。您还可以尝试使用激进的 cflags 甚至使用 Intel C 编译器(我有销售许可证)自己编译 php。 IMO schedtool 与nice 相同,但有更多变量。
In general Linux is very conservative. But you can try schedtool and libmlock. The latter is to lock php in ram to avoid swap. Also you can try swapiness because is mostly very high. You can also try to compile php yourself with aggressive cflags or even with the Intel C Compiler (I've a licence to sell). IMO schedtool is the same as nice but with some more variables.
“睡眠0次”通常是“立即控制产量”的最佳选择。其他方法通常需要在内核模式下运行(甚至内核模式也可能被硬件中断抢占,具体取决于)。
如果睡眠时间为“0”,进程(或线程)将被安排“在不久的将来的某个时间点再次执行”——确切的保证是特定于系统的。实际休眠的最低分辨率取决于环境和系统配置(一般来说,它不会是真正的“0时间”)。在非实时系统(例如 Linux 或 Windows)上通常不能保证产生的确切时间——调度程序将做它想做的事情。
快乐编码。
"sleeping for 0 time" is generally the best that can be done to "yield control right away". Other methods generally require running in kernel-mode (and even kernel mode may be preempted by hardware interrupts, depending).
With a "0 sleep time" the process (or thread) will be scheduled to execute "again some point in the [near] future" -- the exact guarantees are system-specific. The minimum resolution actually slept depends upon environment and system configuration (as a general rule it won't be truly "0 time"). The exact time yielded is generally not guaranteed on non-realtime systems (e.g. Linux or Windows) -- the scheduler will do what it wants to do.
Happy coding.
也许您可以重写应用程序的部分内容,以在 PHP 之外使用命令行工具(不需要您的应用程序逻辑)。
例如,假设您有一个大的 XML 文件要导入。您可以随时尝试使用 CLI 工具
xsltproc
将 XML 文件转换为 CSV 格式,而不是在 PHP 中完成所有操作,这将为您提供一个更适合 PHP 处理的文件。Maybe you can rewrite parts of the application to use command-line tools (that don't need your application logic) outside PHP.
For instance, let's say you have an big XML file to import. Instead of doing everything in PHP, you can always try to convert your XML file into a CSV format using the CLI tool
xsltproc
which would give you a file that would lighter for PHP to work with.我知道这不是主要目的,但是不带参数的yield关键字不会有效地导致处理器屈服于其他进程吗?我一直认为这就是它被称为“yield”的原因。
I know it's not the main purpose, but wouldn't the yield keyword with no arguments effectively cause the processor to yield to other processes? I always thought that's why it was called yield.