略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: pcntl_setpriority

2024-12-22

pcntl_setpriority

(PHP 5, PHP 7, PHP 8)

pcntl_setpriority修改任意进程的优先级

说明

pcntl_setpriority(int $priority, int $pid = getmypid(), int $process_identifier = PRIO_PROCESS): bool

pcntl_setpriority() 设置进程号为 pid的进程的优先级。

参数

priority

priority 通常时-20至20这个范围内的值。默认优先级是0,值越小代表 优先级越高。由于不同的系统类型以及内核版本下优先级可能不同,因此请参考您系统的setpriority(2) 手册以获取详细的规范。

pid

如果没有指定,默认是当前进程的进程号。

process_identifier

PRIO_PGRP(译注:获取进程组优先级), PRIO_USER (译注:获取用户进程优先级)或 PRIO_PROCESS(译注:默认值;获取进程优先级)三者之一。

返回值

成功时返回 true, 或者在失败时返回 false

参见

add a noteadd a note

User Contributed Notes 2 notes

up
2
t dot stobbe at blackdogdev dot com
15 years ago
As for the renice function by leandro dot pereira at gmail dot com, this isn't true.  pcntl_setpriority() doesn't set the nice level of a process, but instead sets the base priority of it.  At first glance this might seem like the same thing, but on a system level, they are actually quite different.

In fact, if you're looking to use pcntl_setpriority() to prioritize your process (a tool or a daemon or what-not), I wouldn't recomend using setpriority at all, but renice it instead.  Let the system manage priorities and you'll end up with the results you were looking for.

This applies only to POSIX based systems only (as does the function presented by leandro dot pereira at gmail dot com as well).
up
-1
leandro dot pereira at gmail dot com
17 years ago
The following snippet may be used under older versions of PHP to provide similar functionality.  Tested only under Linux.

<?php
function _pcntl_setpriority($priority, $pid = 0)
{
       
$priority = (int)$priority;
       
$pid = (int)$pid;

        if (
$priority > 20 && $priority < -20) {
                return
False;
        }
        if (
$pid == 0) {
               
$pid = getmypid();
        }

        return
system("renice  $priority -p $pid") != false;
}

?>

官方地址:https://www.php.net/manual/en/function.pcntl-setpriority.php

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