oasis/multitasking

v1.3.5 2023-06-27 11:44 UTC

This package is auto-updated.

Last update: 2024-08-27 14:07:46 UTC


README

PHP的并行处理支持,使用pcntl库来创建子进程

安装

oasis/multitasking是一个在packagist.org上可用的开源组件。要在您的项目中引入该包,请在项目目录中尝试以下操作

composer require oasis/multitasking

后台工作管理器

如果您想在后台运行某些任务(例如,常被称为工作者的可调用对象),您应该查看BackgroundWorkerManager类。此类提供了以下功能

  • 在后台运行多个工作者(通过派生进程)
  • 有序的工作者队列,限制并发运行的工作者数量
  • 父进程的等待功能

示例

<?php

$man = new BackgroundWorkerManager(2);
$man->addWorker(
    function (WorkerInfo $info) {
        echo sprintf(
            "This is the #%d worker executed out of a total of %d",
            $info->currentWorkerIndex,
            $info->totalWorkers
            );
    },
    10
    ); // add 10 workers to the manager

$man->run(); // all workers will be executed in order, no more than 2 workes will be running at the same time

$man->wait();