{"id":15308186,"url":"https://github.com/yidas/php-worker-dispatcher","last_synced_at":"2025-09-25T14:18:31.346Z","repository":{"id":57086868,"uuid":"280569204","full_name":"yidas/php-worker-dispatcher","owner":"yidas","description":"PHP multi-processing task dispatcher with managing workers","archived":false,"fork":false,"pushed_at":"2024-04-24T03:14:10.000Z","size":156,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-25T10:56:56.566Z","etag":null,"topics":["multiprocessing","pcntl","php","php-cli","task-dispatcher","workers"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-07-18T02:54:20.000Z","updated_at":"2024-04-24T03:14:14.000Z","dependencies_parsed_at":"2025-04-15T01:01:32.435Z","dependency_job_id":"55b0bec4-e951-4fac-a56c-f8025f6a7614","html_url":"https://github.com/yidas/php-worker-dispatcher","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/yidas/php-worker-dispatcher","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-worker-dispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-worker-dispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-worker-dispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-worker-dispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidas","download_url":"https://codeload.github.com/yidas/php-worker-dispatcher/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fphp-worker-dispatcher/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276929886,"owners_count":25730278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-25T02:00:09.612Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["multiprocessing","pcntl","php","php-cli","task-dispatcher","workers"],"created_at":"2024-10-01T08:14:25.234Z","updated_at":"2025-09-25T14:18:31.284Z","avatar_url":"https://github.com/yidas.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.php.net/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://www.php.net/images/logos/php-logo-bigger.png\" height=\"60px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003ePHP Worker Dispatcher\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nPHP multi-processing task dispatcher with managing workers\n\n[![Latest Stable Version](https://poser.pugx.org/yidas/worker-dispatcher/v/stable?format=flat-square)](https://packagist.org/packages/yidas/worker-dispatcher)\n[![License](https://poser.pugx.org/yidas/worker-dispatcher/license?format=flat-square)](https://packagist.org/packages/yidas/worker-dispatcher)\n\n\nFeatures\n--------\n\n- ***Multi-Processing** implementation on native PHP-CLI*\n\n- ***Tasks Dispatching** to each worker process*\n\n- ***Elegant Interface** for setup and use*\n\n---\n\nOUTLINE\n-------\n\n- [Demonstration](#demonstration)\n- [Introduction](#introduction)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Usage](#usage)\n    - [Option](#option)\n        - [callbacks.process](#callbacksprocess)\n        - [callbacks.task](#callbackstask)\n\n---\n\nDEMONSTRATION\n-------------\n\nUse multi-processing to dispatch tasks with generating workers based on CPU cores:\n\n```php\n\\yidas\\WorkerDispatcher::run([\n    'tasks' =\u003e [\"R4NEJ1\", \"F5KH83\", \"...\"],\n    'callbacks' =\u003e [\n        // The callback is for each forked process with decentralized tasks\n        'task' =\u003e function ($config, $workderId, $task) {\n            // $task is one of the `tasks` assigned to each worker, ex. \"F5KH83\" for $workderId is 2\n            $token = $task;\n            $result = file_get_contents(\"https://example/v1/register-by-token/{$token}\");\n        },\n    ],\n]);\n```\n\nUse multi-processing to digest jobs from queue:\n\n```php\n\\yidas\\WorkerDispatcher::run([\n    'tasks' =\u003e false,\n    'callbacks' =\u003e [\n        // The callback is for each forked process\n        'process' =\u003e function ($config, $workderId, $task) {\n            // Get and handle each job from queue in inifite loop (You need to define your own function)\n            while (true) {\n                $result = handleOneJobFromQueue();\n                if ($result === null) {\n                    break;\n                }\n            }\n        },\n    ],\n]);\n```\n\n---\n\nINTRODUCTION\n------------\n\nThis library is implemented by PHP PCNTL control, which provides a main PHP-CLI to fork multiple child processes to share tasks, and even can use for high concurrency application with infinite loop setting.\n\n\u003cimg src=\"https://raw.githubusercontent.com/yidas/php-worker-dispatcher/master/img/introduction.png\" /\u003e\n\n\u003e Since PHP has no shared variables or queue mechanism natively, if you don’t have an external job queue, this library provides a task average dispatcher to simply solve the core distributed processing problem.\n\n---\n\nREQUIREMENTS\n------------\n\nThis library requires the following:\n\n- PHP [PCNTL](https://www.php.net/manual/en/pcntl.installation.php)\n- PHP CLI 5.4.0+\n\n---\n\nINSTALLATION\n------------\n\nRun Composer in your project:\n\n    composer require yidas/worker-dispatcher ~1.0.0\n    \nThen you could use the class after Composer is loaded on your PHP project:\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse yidas\\WorkerDispatcher;\n```\n\n---\n\nUSAGE\n-----\n\nCalling the `run()` method statically with options as argument, WorkerDispatcher will start to dispatch tasks (if any), and then fork the number of workers according to the environment or settings, and wait for all forked processes to complete or terminate the main process.\n\nThe setting example with all options is as following:\n\n```php\n\\yidas\\WorkerDispatcher::run([\n    'debug' =\u003e true,\n    'workers' =\u003e 4,\n    'config' =\u003e ['uri' =\u003e \"/v1/resource\"],\n    'tasks' =\u003e [\"R4NEJ1\", \"F5KH83\", \"...\"],\n    'callbacks' =\u003e [\n        'process' =\u003e function ($config, $workerId, $tasks) {\n            echo \"The number of tasks in forked process - {$workerId}: \" . count($tasks[$workerId - 1]) . \"\\n\";\n        },\n        'task' =\u003e function ($config, $workerId, $task) {\n            echo \"Forked process - {$workerId}: Request to {$config['uri']} with token {$task}\\n\";\n        },\n    ],\n]);\n```\n\n### Options\n\n|Option            |Type     |Deafult      |Description|\n|:--               |:--      |:--          |:--        |\n|debug             |boolean  |false        |Debug mode |\n|workers           |integer  |(auto)       |The number of workers(processes) to fork. \u003cbr\u003e(The default is the same as the number of CPU cores)|\n|config            |multitype|null         |The custom variable used to bring in the callback function|\n|tasks             |multitype|array        |For dispatching to each forked process. *\u003cbr\u003e- Array: Each value of array will be dispatched to all forked processes. \u003cbr\u003e- Integer: The number of loops dispatched to all forked processes. \u003cbr\u003e- false: Perform finite loop.*|\n|[callbacks.process](#callbacksprocess) |callable |nul          |Callback function called after each forked process is created|\n|[callbacks.task](#callbackstask)       |callable |nul          |Callback function called in each task loop of each forked process|\n\n\n#### callbacks.process\n\nCallback function called after each forked process is created\n\n```php\nfunction (multitype $config, integer $workerId, array $tasks)\n```\n\n|Argument          |Type     |Deafult      |Description|\n|:--               |:--      |:--          |:--        |\n|$config            |multitype|null         |The custom variable used to bring in the callback function|\n|$workerId          |integer  |(auto)       |The sequence number of the worker(processes) in current function (Start from 1)|\n|$tasks             |multitype|array        |Tasks array list for the worker(processes) in current function|\n\n#### callbacks.task\n\nCallback function called in each task loop of each forked process\n\n```php\nfunction (multitype $config, integer $workerId, multitype $task)\n```\n\n|Argument          |Type     |Deafult      |Description|\n|:--               |:--      |:--          |:--        |\n|$config            |multitype|null         |The custom variable used to bring in the callback function|\n|$workerId          |integer  |(auto)       |The sequence number of the worker(processes) in current function (Start from 1)|\n|$tasks             |multitype|array        |The value of each tasks array list for each worker(processes) in current function|\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fphp-worker-dispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidas%2Fphp-worker-dispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fphp-worker-dispatcher/lists"}