{"id":19325419,"url":"https://github.com/krypt0nn/asynclib","last_synced_at":"2026-06-22T10:32:06.699Z","repository":{"id":57009430,"uuid":"393697244","full_name":"krypt0nn/asynclib","owner":"krypt0nn","description":"Library which gives you ability to create asynchronous tasks in PHP 7.4+","archived":false,"fork":false,"pushed_at":"2021-08-19T16:45:34.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-11T10:58:17.087Z","etag":null,"topics":["asynchronous","php","threading"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krypt0nn.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}},"created_at":"2021-08-07T13:53:52.000Z","updated_at":"2021-08-26T17:58:31.000Z","dependencies_parsed_at":"2022-08-21T15:10:14.192Z","dependency_job_id":null,"html_url":"https://github.com/krypt0nn/asynclib","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/krypt0nn/asynclib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krypt0nn%2Fasynclib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krypt0nn%2Fasynclib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krypt0nn%2Fasynclib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krypt0nn%2Fasynclib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krypt0nn","download_url":"https://codeload.github.com/krypt0nn/asynclib/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krypt0nn%2Fasynclib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34645681,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"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":["asynchronous","php","threading"],"created_at":"2024-11-10T02:09:59.541Z","updated_at":"2026-06-22T10:32:06.680Z","avatar_url":"https://github.com/krypt0nn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e🚀 Asynclib\u003c/h1\u003e\n\n**Asynclib** is a library that gives you ability to create asynchronous tasks in PHP 7.4+\n\n## Installation\n\n```\ncomposer require krypt0nn/asynclib\n```\n\n## About asynchronous programming\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"https://i.ibb.co/PGn9DX0/Untitled-Diagram.png\"\u003e\u003c/p\u003e\n\n\\* yellows is a tasks ran asynchronously\n\nThis diagram shows the difference between synchronous and asynchronous programs. First ones executing every task in the main process so if you have a task like get a huge file from a website - the program will be paused (lagged) until this task will be not finished. It also means that other tasks in your program will be executed only after this one\n\nAsynchronous programs execute tasks parallel. In Asynclib, every new task runs in another process which means that they will not affect the main program and stop its execution. We can run long high weight tasks async and monitor their states from the main process which will give us a valuable performance boost\n\n## Basic usage\n\n\u003e For practical example you can look at [test](/test) directory\n\u003e \n\u003e Also, keep in mind that this library was developed only for desktop usage. I didn't test it on websites and don't know will it works there or not\n\nEvery task should be written in its file. This file should return the `TaskRunner` object. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\nuse Asynclib\\TaskRunner;\n\nreturn new TaskRunner (function (array $options, TaskRunner $task)\n{\n    // your task code goes here\n});\n```\n\nCallable inside `TaskRunner` constructor takes 2 arguments: `array $options` and `TaskRunner $task`. The first is an array of parameters you give this task in `Task-\u003erun` method when the second is the current `TaskRunner` object\n\nTo run this task in your main code you should use `Task` class:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\nuse Asynclib\\Task;\n\n// Create new task from file\n// and run it\n\n$task = Task::create ('path/to/task/file.php')-\u003erun ([\n    // here you can write some options\n    // that will be available in TaskRunner\n    // also you can run this method\n    // without any arguments\n]);\n```\n\nAfter that you `TaskRunner` will begin its work in another process so it will not affect your code in the current process, stop it, or something like that\n\n`Task` object, which is now stored in our `$task` variable, has a lot of useful methods. But the major is `update`. This method will synchronize task state and process performed events\n\n```php\n/**\n * Task state\n * null  - task is not initialized\n * false - task is still running\n * true  - task is done and you can get its output\n */\n$state = $task-\u003eupdate ();\n```\n\nWe can wait until task will be completed using another major method `wait`\n\n```php\n// PHP 8 syntax\n$task-\u003ewait (milliseconds: 5000);\n```\n\nThis method has 3 arguments: `?int $milliseconds = null`, `int $delay = 100` and `callable $callback = null`. First is the amount of milliseconds after which this function will stop ints work. If you write here a null value - then this method will wait until the task will not be finished. The second argument defines delay between task state updates (`update` method calls). And the last argument is a callable that takes current `Task` object and runs after each task update. If this callable returns `true` (boolean value) - then the wait method will be stopped\n\n```php\n$task-\u003ewait (delay: 1000, callable: function ()\n{\n    echo 'Task is not finished yet' . PHP_EOL;\n});\n```\n\nAnother useful part of `Task` objects is that you can give them an events. For example:\n\n```php\n$task-\u003eon ('some_event', function ($data, Task $task)\n{\n    echo 'Hey! I got a message from task: '. $data . PHP_EOL;\n});\n```\n\nAnd perform this event from `TaskRunner` callable:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\nuse Asynclib\\TaskRunner;\n\nreturn new TaskRunner (function (array $options, TaskRunner $task)\n{\n    for ($i = 0; $i \u003c 5; ++$i)\n    {\n        // Performing event \"some_event\"\n        // in main process\n        $task-\u003eperform ('some_event', 'Now I am at position '. ($i + 1));\n\n        sleep (1);\n    }\n});\n```\n\nEvent `some_event` will be performed when method `update` will be called\n\n`$data` argument can be anything PHP can serialize with function `serialize`: string, number, boolean, array, object...\n\nTask also has a special event which will be called when it will be done\n\n```php\n$task-\u003eonFinished (function ($output, Task $task)\n{\n    echo 'Hey! Task with id '. $task-\u003eid() .\n        ' seems to be finished with output: '. $output . PHP_EOL;\n});\n```\n\nLike `Task`, `TaskRunner` also has events with the same syntax. To receive events from `Task` (main process) and execute them - use `TaskRunner-\u003eupdate` method\n\nTo forcibly interrupt task execution you can call method stop\n\n```php\n$task-\u003estop ();\n```\n\nIt also will set the task state as 0 (just initialized)\n\nOther available methods:\n\n| Name | Type | Description |\n| - | - | - |\n| output | sting | Get task output if it is done |\n| id | string | Get task id |\n| pid | int | Get id of the process which executing this task |\n| state | int | Get task state. 0 - just initialized, 1 - running, 2 - done |\n| initialized | bool | Check if the task is just initialized |\n| running | bool | Check if the task is running |\n| done | bool | Check if the task is done |\n\n## Tasks pools\n\nYou can unite some tasks into one pool and run them together\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\n\nuse Asynclib\\Pool;\n\n// Create tasks pool\n$pool = Pool::create ([\n    'path/to_some_task.php',\n    'path/to_some_another_task.php',\n\n    Task::create ('or_the_task/itself.php')-\u003eonFinished (function ($output)\n    {\n        echo 'Task finished with output: '. $output . PHP_EOL;\n    })\n]);\n\n// Run pool execution\n$pool-\u003erun ();\n\n// Wait untill all tasks in this pool will be finished\n$pool-\u003eupdateWhileExist ();\n```\n\nMethod `update` will update states of all the tasks in this pool and remove the ones which are already finished. It also can take a callback which will be used for every unfinished task. `updateWhileExist` is something similar to `wait` in `Task`, but works like `update`: takes a callable and runs it for every unfinished task until there will be unfinished tasks\n\nYou can get tasks outputs with method `output`\n\n```php\n$task_output = $pool-\u003eoutput ('task_id');\n```\n\nThis method will throw an exception if provided task id does not exist or this task is not finished yet\n\nAlso, you can use `outputOrNull` method if you don't want to work with exceptions. Null value will be returned when the task is not finished or is not exists\n\nMethod `add` can add a new task to the pool\n\n```php\n$pool-\u003eadd (Task::create ('path_to_task.php'));\n```\n\n## Known issues and todos\n\n- [ ] This library technically support IPC protocol, but it is not working properly\n\n\u003cbr\u003e\n\nAuthor: [Nikita Podvirnyy](https://vk.com/technomindlp)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrypt0nn%2Fasynclib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrypt0nn%2Fasynclib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrypt0nn%2Fasynclib/lists"}