略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: proc_nice

2024-11-14

proc_nice

(PHP 5, PHP 7, PHP 8)

proc_nice修改当前进程的优先级

说明

proc_nice(int $increment): bool

proc_nice() 修改当前进程的优先级, 修改量由 increment 参数指定。 increment 为正数会降低当前进程优先级, 反之,为负数会提高优先级。

proc_nice()proc_open() 函数以及和 proc_open() 相关的函数并无什么关系。

参数

increment

新的优先级值,具体的设定取决于所运行的平台。

在 Unix 系统上,较小的值表示较高的优先级,例如:-20, 而正数值表示更低的优先级。

在 Windows 平台上,increment 参数 的含义如下:

优先级 可能的值
高优先级 increment < -9
较高优先级 increment < -4
正常优先级 increment < 5 & increment > -5
较低优先级 increment > 5
低优先级 increment > 9

返回值

成功时返回 true, 或者在失败时返回 false。。 如果发生错误,例如用户无权修改当前进程的优先级, 会生成 E_WARNING 级别的错误。

范例

示例 #1 使用 proc_open() 函数将进程设置为高优先级

<?php
// Highest priority
proc_nice(-20);
?>

更新日志

版本 说明
7.2.0 在 Windows 平台上可用。

注释

注意: 可用性

仅在具有 'nice' 能力的系统上才可以使用 proc_nice() 函数。 下列系统含有 'nice':SVr4, SVID EXT, AT&T, X/OPEN, BSD 4.3。

注意: Windows 平台

proc_nice() 函数会改变 当前 进程优先级,即使 PHP 是使用线程安全模式编译的。

add a noteadd a note

User Contributed Notes 5 notes

up
6
kevin AT REMOVETHIS mrkmg.com
9 years ago
On a Linux system, running apache2 as a non-privileged user you can not increase the niceness of the process after decreasing it. Also, you can not use the apache_child_ terminate either. I found the following does work though:

<?php

//decrease niceness
proc_nice(19);

//kill child process to "reset" niceness
posix_kill( getmypid(), 28 );

?>
up
3
php at richardneill dot org
11 years ago
If a process is reniced, then all its children inherit that niceness. So a PHP script can call proc_nice on itself, then invoke system(), and the command executed via system() will also be niced.

Also worth making a note of ionice. There's no PHP function for this, but it's important. A nice'd program will happily try to chew up all i/o bandwidth with very little CPU usage, it can therefore make the entire computer non-responsive despite the programmer's intention.  Use "ionice -c3"  or see "man ionice"
up
2
Marek
11 years ago
Regarding ionice - on linux the impact of the ionice -c3 class is similar to that of nice, because the CPU "niceness" is taken into account when calculating the io niceness.
up
-1
phil_php at zieaon dot com
1 year ago
It is important to note that this is a relative change. I didn't read the description properly and couldn't figure out why setting proc_nice(0) didn't take the forked children back to 0!
For example if you run:
<?php
proc_nice
(-5);
proc_nice(0); // will have no effect
proc_nice(5); // will take the niceness back to 0

?>

In PHP CLI under Debian (and probably many other Linux flavours) you can read the 'niceness' from the proc filesystem.  (There may be a PHP command that gives this info but there doesn't seem to be a link to it on this page.)
E.g
<?php
$Current_Niceness_Value
= intval(explode(" ",file_get_contents("/proc/".getmypid()."/stat"))[18]);

// Note: Older versions of Linux return an unsigned integer which has to be converted to a signed integer.
$Current_Niceness_Value = unpack("l",pack("L",intval(explode(" ",file_get_contents("/proc/".getmypid()."/stat"))[18])))[1];

?>
up
-1
pandi at home dot pl
13 years ago
Simple function for check process nice, by default returns nice of current process:

<?php

public static function getProcessNice ($pid = null) {
    if (!
$pid) {
       
$pid = getmypid ();
    }
       
   
$res = `ps -p $pid -o "%p %n"`;
       
   
preg_match ('/^\s*\w+\s+\w+\s*(\d+)\s+(\d+)/m', $res, $matches);
       
    return array (
'pid' => (isset ($matches[1]) ? $matches[1] : null), 'nice' => (isset ($matches[2]) ? $matches[2] : null));
}

?>

官方地址:https://www.php.net/manual/en/function.proc-nice.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3