{"id":21899146,"url":"https://github.com/anhoder/php-async","last_synced_at":"2026-05-20T14:41:45.995Z","repository":{"id":56943611,"uuid":"200464579","full_name":"anhoder/php-async","owner":"anhoder","description":"PHP异步回调的简单实现","archived":false,"fork":false,"pushed_at":"2019-08-19T01:25:39.000Z","size":1767,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-27T06:15:11.274Z","etag":null,"topics":["async","php","php-async","php-fpm"],"latest_commit_sha":null,"homepage":null,"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/anhoder.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":"2019-08-04T07:48:52.000Z","updated_at":"2022-08-25T15:27:24.000Z","dependencies_parsed_at":"2022-08-21T02:10:17.925Z","dependency_job_id":null,"html_url":"https://github.com/anhoder/php-async","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhoder%2Fphp-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhoder%2Fphp-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhoder%2Fphp-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anhoder%2Fphp-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anhoder","download_url":"https://codeload.github.com/anhoder/php-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244911531,"owners_count":20530641,"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":["async","php","php-async","php-fpm"],"created_at":"2024-11-28T14:38:15.761Z","updated_at":"2026-05-20T14:41:45.961Z","avatar_url":"https://github.com/anhoder.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-async\n\nPHP异步回调的简单实现\n\n**php-fpm模式的使用，仍在测试，可能会出现未知问题。**\n\n* php-fpm模式下已解决的问题：\n\n\u003e 解决异步进程执行完成后，成为僵尸进程的问题\n\u003e\n\u003e 经测试，异步进程的fork不受php-fpm配置的进程数限制\n\n\n## Requirement / 依赖\n\n* ext-pcntl\n* php \u003e 5.6\n\n## Installation / 安装\n\n```sh\ncomposer require alanalbert/php-async\n```\n\n## Usage / 使用\n\n\u003e 该库可以工作在php-fpm或cli模式下。（但是，每个异步任务会创建一个进程进行处理，直至任务完成后才会被**彻底杀死**）\n\n**现在可以使用[PHP Async Manager](https://github.com/AlanAlbert/php-async-manager)命令行工具进行异步任务管理。**\n\n### 使用实例\n\n```php\nrequire './vendor/autoload.php';\n\nuse Async\\DaemonProcess;\nuse Async\\Job;\n\n// 异步操作任务\n$job = new Job();\n// 设置异步任务\n$job-\u003esetJob(function () {\n  sleep(5);\t\t\n});\n// 设置回调函数\n$job-\u003esetCallback(function () {\n  file_put_contents('处理完成.txt', '处理完成');\t\t\n});\n\n// 实例化异步进程\n$daemon = new DaemonProcess($job);\n$daemon-\u003erun();\n```\n\n### Async\\DaemonProcess\n\n守护进程类，该类提供的方法有：\n\n```php\n// 构造函数，接收实现Async\\Contract\\JobInterface接口的类的实例\n__construct(Async\\Contract\\JobInterface $job): void\n\n// 运行守护进程\nrun(): void\n```\n\n### Async\\Contract\\JobInterface\n\n异步任务接口，该接口很简单，只需要实现两个方法即可：\n\n```php\n// 异步任务\njob(): void\n// 回调任务\ncallback(): void\n```\n\n### Async\\Job\n\n该类实现了`Async\\Contract\\JobInterface`接口，使用起来更方便，其提供的方法有：\n\n```php\n// 设置异步任务\n// $job可以为callable或Closure类型\n// $params为异步任务需要使用的参数，会在执行时，传入给异步任务\nsetJob($job, $params = null): void\n\n// 设置回调函数\n// $callback可以为callable或Closure类型\n// $params为回调函数需要使用的参数，会在执行时，传入给回调函数\nsetCallback($callback, $params = null): void\n```\n\n### 其他\n\n* 在使用该库执行异步任务时，会生成日志文件，位于`/tmp/php-async-log/`下\n* 如果不使用`Async\\Job`类，也可以自定义类并实现`Async\\Contract\\JobInterface`接口\n\n## TODO\n\n* ...\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanhoder%2Fphp-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanhoder%2Fphp-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanhoder%2Fphp-async/lists"}