{"id":20441733,"url":"https://github.com/mix-php/worker-pool","last_synced_at":"2026-03-06T08:32:52.737Z","repository":{"id":57017814,"uuid":"278598502","full_name":"mix-php/worker-pool","owner":"mix-php","description":"Swoole-based worker pool, coroutine pool / 基于 Swoole 的工作池，协程池","archived":false,"fork":false,"pushed_at":"2021-07-21T01:55:31.000Z","size":15,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-18T05:42:04.166Z","etag":null,"topics":["coroutine-pool","pool","worker-pool"],"latest_commit_sha":null,"homepage":"https://openmix.org/mix-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-10T09:51:33.000Z","updated_at":"2024-12-16T14:56:28.000Z","dependencies_parsed_at":"2022-08-22T12:00:27.167Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/worker-pool","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/mix-php/worker-pool","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fworker-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fworker-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fworker-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fworker-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/worker-pool/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fworker-pool/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30167963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T07:56:45.623Z","status":"ssl_error","status_checked_at":"2026-03-06T07:55:55.621Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["coroutine-pool","pool","worker-pool"],"created_at":"2024-11-15T09:34:28.940Z","updated_at":"2026-03-06T08:32:52.707Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n# Mix Worker Pool\n\nSwoole-based worker pool, coroutine pool\n\n基于 Swoole 的工作池，协程池\n\n\u003e go 版本：https://github.com/mix-go/xwp\n\n## Installation\n\n```\ncomposer require mix/worker-pool\n```\n\n## 单次调度\n\n- 如果不想阻塞执行，可以使用 `$pool-\u003estart()` 启动\n\n```php\n$jobQueue = new Swoole\\Coroutine\\Channel(200);\n$maxWorkers = 100;\n$handler = function ($data) {\n    // do something\n};\n$pool = new Mix\\WorkerPool\\WorkerPool($jobQueue, $maxWorkers, $handler);\n\ngo(function () use ($jobQueue, $pool) {\n    // 投放任务\n    for ($i = 0; $i \u003c 1000; $i++) {\n        $jobQueue-\u003epush($i);\n    }\n    // 停止\n    $pool-\u003estop();\n});\n\n$pool-\u003erun(); // 阻塞等待\n```\n\n上面是采用闭包处理任务，也可以使用对象处理任务\n\n```php\nclass FooHandler implements \\Mix\\WorkerPool\\RunInterface\n{\n    public function do($data): void\n    {\n        // do something\n    }\n}\n$pool = new Mix\\WorkerPool\\WorkerPool($jobQueue, $maxWorkers, new FooHandler());\n```\n\n## 常驻调度\n\n\u003e 适合处理 MQ 队列的异步消费\n\n以 Redis 作为 MQ 为例：\n\n```php\n$maxWorkers = 20;\n$maxQueue = 10;\n$jobQueue = new Swoole\\Coroutine\\Channel($maxQueue);\n$handler = function ($data) {\n    // do something\n};\n$pool = new Mix\\WorkerPool\\WorkerPool($jobQueue, $maxWorkers, $handler);\n\n$quit = new Swoole\\Coroutine\\Channel();\nforeach ([SIGHUP, SIGINT, SIGTERM] as $signal) {\n    Swoole\\Process::signal($signal, function () use ($quit) {\n        $quit-\u003epush(true);\n    });\n}\n\ngo(function () use ($jobQueue, $pool, $quit) {\n    // 投放任务\n    while (true) {\n        if (!$quit-\u003eisEmpty()) {\n            $pool-\u003estop();\n            return;\n        }\n        try {\n            $data = $redis-\u003ebrPop(['test'], 1);\n        } catch (\\Throwable $ex) {\n            // print log\n            $pool-\u003estop();\n            return;\n        }\n        if (!$data) {\n            continue;\n        }\n        $data = array_pop($data); // brPop命令最后一个键才是值\n        $jobQueue-\u003epush($data);\n    }\n});\n\n$pool-\u003erun(); // 阻塞等待\n```\n\n## 异常处理\n\n闭包或者对象 `do` 方法中执行的代码，可能会抛出异常，必须要使用 `try/catch` 避免协程退出\n\n```php\nclass FooHandler implements \\Mix\\WorkerPool\\RunInterface\n{\n    public function do($data): void\n    {\n        try {\n            // do something\n        } catch (\\Throwable $ex){\n            // print log\n        }\n    }\n}\n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fworker-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fworker-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fworker-pool/lists"}