{"id":17123061,"url":"https://github.com/oliverde8/php-asynchronousjobs","last_synced_at":"2025-07-25T02:32:26.467Z","repository":{"id":57030990,"uuid":"51643392","full_name":"oliverde8/PHP-AsynchronousJobs","owner":"oliverde8","description":"Execute new php instances easily as you would threads.","archived":false,"fork":false,"pushed_at":"2016-03-14T18:53:07.000Z","size":33,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-18T09:52:24.530Z","etag":null,"topics":["async","parallel","php","thread"],"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/oliverde8.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":"2016-02-13T11:34:51.000Z","updated_at":"2017-12-07T20:31:07.000Z","dependencies_parsed_at":"2022-08-23T17:40:58.807Z","dependency_job_id":null,"html_url":"https://github.com/oliverde8/PHP-AsynchronousJobs","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/oliverde8%2FPHP-AsynchronousJobs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliverde8%2FPHP-AsynchronousJobs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliverde8%2FPHP-AsynchronousJobs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliverde8%2FPHP-AsynchronousJobs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oliverde8","download_url":"https://codeload.github.com/oliverde8/PHP-AsynchronousJobs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227507579,"owners_count":17782011,"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","parallel","php","thread"],"created_at":"2024-10-14T18:24:39.653Z","updated_at":"2024-12-01T07:21:39.673Z","avatar_url":"https://github.com/oliverde8.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Asynchronous - Jobs\n\nPHP AsynchronousJobs is a small library allowing to run PHP code in parallel of your main code. In some ways it works like threads but it doesen't use the proper system libraries to do so. It is a workaround \n\n[![Build Status](https://travis-ci.org/oliverde8/PHP-AsynchronousJobs.svg?branch=master)](https://travis-ci.org/oliverde8/PHP-AsynchronousJobs)\n[![Latest Stable Version](https://poser.pugx.org/oliverde8/asynchronous-jobs/v/stable)](https://packagist.org/packages/oliverde8/asynchronous-jobs) [![Total Downloads](https://poser.pugx.org/oliverde8/asynchronous-jobs/downloads)](https://packagist.org/packages/oliverde8/asynchronous-jobs) [![Latest Unstable Version](https://poser.pugx.org/oliverde8/asynchronous-jobs/v/unstable)](https://packagist.org/packages/oliverde8/asynchronous-jobs) [![License](https://poser.pugx.org/oliverde8/asynchronous-jobs/license)](https://packagist.org/packages/oliverde8/asynchronous-jobs)\n\nThe library was created to work on windows \u0026 linux systems without the need of installing php pthreads extension. **The library must not be considered as replacement to threads !!** The way the library works isn't optimized to for that. \n\nIt is built in the purpose of doing long tasks in parallel. A good example would be to download filess while the main script is serving informations to users. The library is ment to be used for command line tools \u0026 deamons. \n\n## Why\n\nEven throught pthreads is fantastic many servers don't have it installed, and therefore adding a dependency to that can be blocking. In this case the library was built for the [ml-expansion](http://ml-expansion.com/) project which is used by people that wishes to have something that just works. This library answers to that question.\n\nIf you need to run a few threads time to time without wanting to add a dependency to pthreads this library might just be want you need.\n\n## FAQ \n\nhttps://github.com/oliverde8/PHP-AsynchronousJobs/wiki/Faq\n\n## Usage \n\nFirst you need to create a job : \n```\nclass Sleep extends Job\n{\n    public $time = 1;\n\n    /**\n     * Method called by the new instance to run the job.\n     *\n     * @return mixed\n     */\n    public function run()\n    {\n        sleep($this-\u003etime);\n    }\n\n    /**\n     * Method called by the original instance when the job has ran.\n     *\n     * @return mixed\n     */\n    public function end()\n    {\n      $time = $this-\u003etime;\n      echo \"I end after : $time!\";\n    }\n}\n```\n\nThen you can create a job : \n```\n$job1 = new Sleep();\n$job1-\u003esleep = 3;\n```\n\nLet's create a second job as well.\n```\n$job2 = new Sleep();\n$job2-\u003esleep = 2;\n```\n\nNow execute the jobs\n```\n$job1-\u003estart()\n$job2-\u003estart()\n\n// And wait for the end\nsleep (4);\n```\n\nYou should see first \"I end after 2\" then the message \"I end after 3\"\n\n### Waiting for ongoing jobs \n\nOnce you started some jobs you may decide you need to wait for them to finish. \n\nSo once you have started your jobs :\n```\n$job1-\u003estart()\n$job2-\u003estart()\n// ....\n```\n\nYou can use wait all : \n```\nJobRunner::getInstance()-\u003ewaitForAll(1);\n```\nThis will block you instance until all jobs habe finished their execution. waitForAll takes in parameters the sleep time. So if you know your jobs takes a few jours to run you can increase the sleep time to a few minutes to prevent IO to be over used.\n\nYou may also wish for just one job to finish : \n```\n$job1-\u003ewait();\n```\nThe process will be blocked until the job1 is finished. \n\n### Making a Curl request. \nThis is a very simple implementation for doing curl queries. **It should be improved !** But well it can be built open the job executions.\n```\n$curlJob = new Curl();\n$curlJob-\u003esetMethod('GET');\n$curlJob-\u003esetUrl('http://jsonplaceholder.typicode.com/posts');\n\n$curlJob-\u003estart();\nJobRunner::getInstance()-\u003ewaitForAll(1);\n\n$info = $curlJob-\u003egetCurlInfo();\n$response = $curlJob-\u003egetResponse()\n```\n\nYou can of crouse pass some parameters and do a POST queries as well. \n\n### Having callbacks\nSomething else you can do is place callbacks on your jobs in order to have a function called when the job is done.\n\n```\npublic function testCallback()\n{\n    $curlJob = new CallbackCurl();\n    $curlJob-\u003esetMethod('GET');\n    $curlJob-\u003esetUrl('http://jsonplaceholder.typicode.com/posts');\n    $curlJob-\u003esetCallback(array($this, '_testCallbackCallback'));\n\n    $curlJob-\u003estart();\n    JobRunner::getInstance()-\u003ewaitForAll(1);\n    echo \"You should then see this !\\n\"\n}\n\npublic function _testCallbackCallback(Job $curlJob)\n{\n    echo \"You should first see this !\\n\";\n}\n```\n\n### Custom settings\nYo have custom settings you must call \n```\nJobRunner::getInstance()\n```\nBefore running initiating any jobs before, it needs to be your first call !!\n\nThe methods can take the fallowing parameters : \n* **$id** Allows you to share instances between multiple process, leave it null if you don't understand it should rarely need changing. \n* **$phpExecutable** Path to the php executable, can be an issue on windows, on linux php alone should suffice. \n* **$tmpPath** Path to put the temporary files used to synchronize the process. It needs to be writable by the process.\n\n### Stuff to now\n\n#### Ignored variables : \nAll variables you set in a job is transfered to the new execution(thread) once the job starts and are then updated when to job finishes with the new values coming from the job. \nThis varibles may be privete or public, they are going to be serialized and passed to the new execution. Once the execution finishes they will be serialized again and sent back to the main execution.\n\nIn some cases we may wish to have values available only on the main process or on the new execution. All variables prefixed by a double underscore(\"_\") will be ignored during transfer. \n\nExample :\n```\nprivate $__test = 'tt';\n```\n\n## TODO In the future\n* Redis support (so much cooler \u0026 faster)\n    * Note for my self : I need to separate the current JobRunner's content so that the data management section can be separated from the logic. \n* Semaphore support\n* Shared memory between jobs \u0026 instance\n    * The jobs will do ```-\u003epush()``` when they wish to share some new data, and on the main process we will call ```-\u003epop()``` on the job to get the latest information. The purpose for this isn't to sync a lot of data but just keep track of progress or something. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliverde8%2Fphp-asynchronousjobs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foliverde8%2Fphp-asynchronousjobs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliverde8%2Fphp-asynchronousjobs/lists"}