{"id":13793035,"url":"https://github.com/SegmentFault/SimpleFork","last_synced_at":"2025-05-12T17:31:43.263Z","repository":{"id":83801487,"uuid":"64362034","full_name":"segmentfault/SimpleFork","owner":"segmentfault","description":"一个最精简的php多进程控制库","archived":false,"fork":false,"pushed_at":"2018-04-20T15:45:06.000Z","size":13,"stargazers_count":102,"open_issues_count":1,"forks_count":30,"subscribers_count":22,"default_branch":"master","last_synced_at":"2024-08-04T22:19:18.565Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/segmentfault.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}},"created_at":"2016-07-28T03:41:39.000Z","updated_at":"2024-07-24T06:21:55.000Z","dependencies_parsed_at":"2024-01-07T05:12:30.500Z","dependency_job_id":"b3ebf57b-215c-4d0e-b373-73fdce050f51","html_url":"https://github.com/segmentfault/SimpleFork","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentfault%2FSimpleFork","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentfault%2FSimpleFork/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentfault%2FSimpleFork/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/segmentfault%2FSimpleFork/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/segmentfault","download_url":"https://codeload.github.com/segmentfault/SimpleFork/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225144934,"owners_count":17427894,"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","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":[],"created_at":"2024-08-03T22:01:20.816Z","updated_at":"2024-11-18T07:30:18.018Z","avatar_url":"https://github.com/segmentfault.png","language":"PHP","funding_links":[],"categories":["类库"],"sub_categories":["任务"],"readme":"# SimpleFork\n\n一个最精简的php多进程控制库。它不依赖任何扩展以及其它的库，可以让你方便地利用系统的多个cpu来完成一些异步任务。我们封装了主进程和子进程之间的通信，以及日志打印，还有错误处理。\n\n## 如何使用\n\n### 安装\n\n因为只有一个文件，你可以将其拷贝到任意位置。\n\n### 使用\n\n#### 循环调用的时候(比如从任务队列获取任务常驻内存)\n\n```php\n$sf = new SimpleFork(2, 'my-process'); // 2代表子进程数, 'my-process'是进程的名字\n\n$sf-\u003emaster(function ($sf) {\n    // 主进程的方法请包裹在master里\n    while ($sf-\u003eloop(100)) { // 100为等待的毫秒数\n        $sf-\u003esubmit('http://www.google.cn/', function ($data) { // 使用submit方法将其提交到一个空闲的进程，如果没有空闲的，系统会自动等待\n            echo $data;\n        });\n    }\n})-\u003eslave(function ($url, $sf) {\n    $sf-\u003elog('fetch %s', $url); // 使用内置的log方法，子进程的log也会被打印到主进程里\n    return http_request($url);  // 直接返回数据，主进程将在回调中收到\n});\n```\n\n#### 一次调用的时候(比如有一个很大的工作需要分片处理)\n\n```php\n$sf = new SimpleFork(5, 'map-reduce');\n\n$sf-\u003emaster(function ($sf) {\n    $sf-\u003esubmit([0, 10000]);\n    $sf-\u003esubmit([10000, 20000]);\n    $sf-\u003esubmit([20000, 30000]);\n    $sf-\u003esubmit([30000, 40000]);\n    $sf-\u003esubmit([40000, 50000]);\n\n    $sf-\u003ewait();    // 等待所有任务执行完毕, 可以带一个timeout参数代表超时时间毫秒数, 超过后将强行终止还没完成的任务并返回\n})-\u003eslave(function ($params, $sf) {\n    list ($from, $to) = $params;\n    file_read_by_line($from, $to, 'example.txt');\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSegmentFault%2FSimpleFork","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSegmentFault%2FSimpleFork","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSegmentFault%2FSimpleFork/lists"}