如何计算PHP脚本(Linux)中的处理器核心数? [英] How to calculate number of processor cores in PHP script (linux)?

查看:37
本文介绍了如何计算PHP脚本(Linux)中的处理器核心数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pthreads进行多线程处理.我正在用构造函数创建池.第一个参数是工人数.

I'm trying to use pthreads for multithreading. I'm creating pool with constructor. First parameter is number of Workers.

$pool = new Pool(8, 'WebWorker');

我想自动检测处理器核心的数量.像这样:

I want to detect count of processor cores automatically. Something like this:

$pool = new Pool(get_processor_cores_number(), 'WebWorker');

PHP怎么可能?

推荐答案

如果服务器是Linux计算机,则可以使用以下代码段进行操作:

If the server is a Linux machine you can do it with the following snippet:

$ncpu = 1;

if(is_file('/proc/cpuinfo')) {
    $cpuinfo = file_get_contents('/proc/cpuinfo');
    preg_match_all('/^processor/m', $cpuinfo, $matches);
    $ncpu = count($matches[0]);
}

这篇关于如何计算PHP脚本(Linux)中的处理器核心数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆