{"id":33982017,"url":"https://github.com/superrbstudio/async","last_synced_at":"2025-12-13T03:53:59.863Z","repository":{"id":56802570,"uuid":"101316416","full_name":"superrbstudio/async","owner":"superrbstudio","description":"A simple PHP library for creating asynchronous processes, and handling inter-process communication via sockets.","archived":false,"fork":false,"pushed_at":"2022-05-26T09:01:46.000Z","size":31,"stargazers_count":8,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-09-07T01:39:44.901Z","etag":null,"topics":["async","asynchronous","fork","php","php-library","socket","socket-communication","subprocess","threads"],"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/superrbstudio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE-OF-CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-24T16:35:28.000Z","updated_at":"2022-08-17T04:54:31.000Z","dependencies_parsed_at":"2022-08-17T05:30:41.256Z","dependency_job_id":null,"html_url":"https://github.com/superrbstudio/async","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/superrbstudio/async","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superrbstudio%2Fasync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superrbstudio%2Fasync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superrbstudio%2Fasync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superrbstudio%2Fasync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/superrbstudio","download_url":"https://codeload.github.com/superrbstudio/async/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superrbstudio%2Fasync/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27699675,"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-12-13T02:00:09.769Z","response_time":147,"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":["async","asynchronous","fork","php","php-library","socket","socket-communication","subprocess","threads"],"created_at":"2025-12-13T03:53:59.124Z","updated_at":"2025-12-13T03:53:59.856Z","avatar_url":"https://github.com/superrbstudio.png","language":"PHP","readme":"# Async\n\nA simple PHP library for creating asynchronous processes, and handling inter-process communication via sockets.\n\n## Installation\n\n```sh\ncomposer require superrb/async\n```\n\n## Usage\n\n### Getting Started\n\nThe easiest way to use this package is to use the `async` helper. This simply runs the supplied closure asynchronously within a forked process. The closure must return a boolean to indicate success/failure of the subprocess, and have the return type `bool`, as this is used to set the exit state of the process.\n\n```php\necho 1.\"\\n\";\n\nasync(function(): bool {\n\tsleep(1);\n\techo 2.\"\\n\";\n\treturn true;\n});\n\necho 3.\"\\n\";\n\n// Output\n1\n3\n2\n```\n\nAny arguments passed to `async` will be passed straight to the forked process.\n\n```php\nasync(function($i, $j): bool {\n\techo $i.\"\\n\";\n\techo $j.\"\\n\";\n\treturn true;\n}, 1, 2);\n\n// Output\n1\n2\n```\n\n### Reusable closures\n\nYou can reuse a closure, by manually constructing an instance of `Superrb\\Async\\Handler`\n\n```php\n$handler = new Superrb\\Async\\Handler(function(int $i): bool {\n\techo $i;\n\treturn true;\n});\n\nfor ($i = 0; $i \u003c 10; $i++) {\n\t$handler-\u003erun($i);\n}\n\n// Processing will pause here until all asynchronous processes\n// have completed. $success is true if all processes returned\n// true, otherwise it is false\n$success = $handler-\u003ewaitAll();\n```\n\nYou can run synchronous code within forked processes by passing `false` as the second argument to the `Handler` constructor. This is useful for running long processes such as imports, as any memory consumed within the loop is dumped at the end of the process.\n\n```php\n$handler = new Superrb\\Async\\Handler(function(int $i): bool {\n\techo $i;\n\treturn true;\n}, false);\n\nfor ($i = 0; $i \u003c 10; $i++) {\n\t// The loop will pause whilst the process runs, and continue\n\t// when it is completed. $success is the return value of the\n\t// closure\n\t$success = $handler-\u003erun($i);\n}\n```\n\n### Inter-process communication\n\n#### Sending messages to the parent\n\n`async` uses channels and socket pairs to allow for communication between the child and parent process. Communication is one way - you can pass messages from the child process back to the main process using `$this-\u003esend()` within your closure. Messages can be strings, objects or arrays.\n\n```php\n$handler = new Superrb\\Async\\Handler(function(int $i): bool {\n\t$this-\u003esend('Hi from process '.$i);\n\treturn true;\n});\n\nfor ($i = 0; $i \u003c 10; $i++) {\n\t$handler-\u003erun($i);\n}\n\n$handler-\u003ewaitAll();\n```\n\n#### Reading messages\n\nMessages can then be read by calling the generator `$handler-\u003egetMessages()` once processing has completed. For asynchronous processes, the messages will always be received **in the order the handlers were run** regardless of which process finished first.\n\n```php\nforeach($handler-\u003egetMessages() as $message) {\n\techo $message.\"\\n\";\n}\n\n// output\nHi from process 1\nHi from process 2\nHi from process 3\n...\n```\n\nFor synchronous processes, you can read messages at the end of each process. If you do this, you'll need to clear the message pool manually after reading them.\n\n```php\nfor ($i = 0; $i \u003c 10; $i++) {\n\t$handler-\u003erun($i);\n\t\n\tforeach($handler-\u003egetMessages() as $message) {\n\t\techo $message.\"\\n\";\n\t}\n\t\n\t$handler-\u003eclearMessages();\n}\n```\n\n#### Increasing the communication buffer\n\nBy default messages are sent within a 1024-byte message buffer, and attempting to send data larger than this will result in an exception. You can increase/decrease the buffer by calling `setMessageBuffer` on the handler.\n\n```php\n$handler-\u003esetMessageBuffer(4096);\n```\n\n## Contributing\n\nAll contributions are welcome, and encouraged. Please read our [contribution guidelines](CONTRIBUTING.md) and [code of conduct](CODE-OF-CONDUCT.md) for more information.\n\n## License\n\nCopyright (c) 2017 [Superrb Studio](https://superrb.com) \u003ctech@superrb.com\u003e\n\nAsync is licensed under The MIT License (MIT)\n\n## Team\n\n* James Dinsdale (@molovo)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperrbstudio%2Fasync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuperrbstudio%2Fasync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperrbstudio%2Fasync/lists"}