{"id":34552650,"url":"https://github.com/ralphmoran/paralleljobs","last_synced_at":"2026-04-16T17:02:02.809Z","repository":{"id":53538439,"uuid":"335049380","full_name":"ralphmoran/paralleljobs","owner":"ralphmoran","description":"Vanilla #PHP does not support #asynchronous #jobs or #multitask processes. Here I can explain how you can achieve it when you run your script on a Unix/Linux OS.","archived":false,"fork":false,"pushed_at":"2021-03-30T19:00:33.000Z","size":65,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-09-08T00:13:03.194Z","etag":null,"topics":["asynchronous","jobs","linux","multitasking","parallel","php","unix"],"latest_commit_sha":null,"homepage":"","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/ralphmoran.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":"2021-02-01T18:43:48.000Z","updated_at":"2023-09-08T00:13:03.195Z","dependencies_parsed_at":"2022-08-23T14:30:45.309Z","dependency_job_id":null,"html_url":"https://github.com/ralphmoran/paralleljobs","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"purl":"pkg:github/ralphmoran/paralleljobs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralphmoran%2Fparalleljobs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralphmoran%2Fparalleljobs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralphmoran%2Fparalleljobs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralphmoran%2Fparalleljobs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ralphmoran","download_url":"https://codeload.github.com/ralphmoran/paralleljobs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ralphmoran%2Fparalleljobs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27998473,"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-24T02:00:07.193Z","response_time":83,"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","jobs","linux","multitasking","parallel","php","unix"],"created_at":"2025-12-24T08:02:50.309Z","updated_at":"2025-12-24T08:03:27.891Z","avatar_url":"https://github.com/ralphmoran.png","language":"PHP","readme":"# Parallel jobs - Asynced processes - Threads\nVanilla #PHP does not support #asynchronous or #background processes or #multitask feature. Here, I can explain how I've accomplished this well-required feature on *NIX OS.\n\nThe use of backticks (`) in your PHP script is not recommended because they make your code less portable but there are many options to \"achieve\" that feature, like building a VirtualBox and defining the best Linux distro for your needs where your code is going to run on, that way, your code can accomplish that.\n\n## Thread class\n\n### Instantiation\n\n```php\n$thread = new Thread(); # Default thread group is assigned\n\nor \n\n$thread = new Thread([\n        'group' =\u003e 'spx', # 'spx' is a general group label\n        'execs' =\u003e [] # It overwrites, when it's not empty, the default execs. By default, there is just one: php\n    ]);\n```\n\n### Dispatching a single thread (default group)\n\nIt registers and creates a new thread in background for 'async_process.php' with one argument 'single_command_default_group'. Thread object adds a unique hash to each new asynced thread for further handling.\n\n```php\n$thread-\u003edispatch( 'async_process.php single_command_default_group' );\n```\n\n### Dispatching a list of threads, assigning a group name to this colection, registering and dispatching them all.\n\n```php\n#-----------------------------------------------------------------\n# Register threads, assing a group \"g1\", and dispatch them all.\n$thread\n    -\u003egroup( 'g1' ) # 'g1' overwrites general group label (highest priority)\n    -\u003eregister(\n    [\n        ['async_process.php 1 2 3 4 5'],\n        ['async_process.php 1 2 3'],\n        ['async_process.php 1'],\n    ]\n    );\n    -\u003edispatchAll(); # Calling dispatch() or dispatchAll() will blank group name.\n```\n\n```php\n#-----------------------------------------------------------------\n# Register a single thread, assing it to a group \"g2\", and dispatch it.\n$thread\n        -\u003egroup( 'g2' )\n        -\u003eregister( 'async_process.php register_dispatchAll' );\n        -\u003edispatchAll(); # Calling dispatch() or dispatchAll() will blank group name.\n\n```\n\n```php\n#-----------------------------------------------------------------\n# Add to a group \"g3\", register them, and dispatch a collection of commands.\n$thread\n        -\u003egroup( 'g3' )\n        -\u003edispatch(\n        [\n            ['async_process.php d1'],\n            ['async_process.php d2'],\n            ['async_process.php d3'],\n        ]\n    ); # Calling dispatch() or dispatchAll() will blank group name.\n\n```\n\n```php\n#-----------------------------------------------------------------\n# Dispatch a single command in series, it takes the last assigned group, in this case \"g3\".\nfor( $i=0; $i\u003c=2; $i++ )\n    $thread-\u003edispatch( 'async_process.php single_command_last_group' . $i ); # Calling dispatch() or dispatchAll() will blank group name.\n\n$thread-\u003edispatchAll(); # Calling dispatch() or dispatchAll() will blank group name\n```\n\n### Get all registered threads\n\n```php\n#-----------------------------------------------------------------\n# Get all registered threads created by Thread class\nprint_r( $thread-\u003egetRegisteredThreads() );\n```\n\n### Get all running threads or by group or word\n\n```php\n#-----------------------------------------------------------------\n# Get ONLY running threads by group or word created by Thread class\n$thread-\u003egetRunningThreads('spx');\n$thread-\u003egetRunningThreads('g1');\n$thread-\u003egetRunningThreads('g2');\n$thread-\u003egetRunningThreads('g3');\n\n# Get ONLY all running threads created by Thread class\n$thread-\u003egetRunningThreads();\n```\n\n## Killing all threads or by group, word\n\n```php\n#-----------------------------------------------------------------\n# Kill threads by groups or word created by Thread class\n$thread-\u003ekillAll('spx');\n$thread-\u003ekillAll('g1');\n$thread-\u003ekillAll('g2');\n$thread-\u003ekillAll('g3');\n\n# Kill all threads created by Thread class\n$thread-\u003ekillAll();\n```\n\n\n## Important notes for me\n\n### How to make async instances of a PHP script\n\n```php\n\u003c?php\n\n# Make 10 async jobs for async_process.php\nfor( $i = 0; $i \u003c 10; $i++ ){\n    \n    $id = $i . '_' . time();\n\n    # The amp \"\u0026\" char at the end makes the magic here ;)\n    `php async_process.php {$id} \u003e /dev/null \u0026`; \n}\n```\n\n## How to wake the deamon up\n```php\n\u003c?php\n\n# Again, it's important the last amp \"\u0026\" char to make it asynced and pass this call to Unix/Linux kernel\n`php deamon.php \u003e /dev/null \u0026`;\n```\n\n### Other userful commands\n\n####  Get all running Unix/Linux instances that contain \"word\"\n```php\n\u003c?php\n\n# List all running processes that contain word in them, including the actual grep process\n$instances = `ps -wef | grep \"{word}\"`;\n\n# Rules out the actual grep process from results\n$instances = `ps -wef | grep \"{word}\" | grep -v grep`; \n\n# Returns ONLY running process with specific columns: PID, ELAPSED TIME, COMMAND, and rules out grep process\n$instances = `ps -ro pid,etime,command | grep \"{word}\" | grep -v grep`; \n```\n\nI.e.\n```php\n\u003c?php\n\n$instances = `ps -wef | grep \"async_process.php\"`;\n$instances = `ps -wef | grep \"async_process.php\" | grep -v grep`;\n$instances = `ps -ro pid,etime,command | grep \"async_process.php\" | grep -v grep`;\n```\n\n#### Returns the elapsed time in secs from a given PID\n```php\n\u003c?php\n\n$pid_elapsed_time = trim(`ps -p {$pid} -o etime=`);\n```\n\nI.e.\n```php\n\u003c?php\n\n# It returns 03:07 which is 3 mins and 7 secs\n$pid_elapsed_time = trim(`ps -p 37809 -o etime=`);\n```\n\n\n#### Returns detailed data of the Linux server.\n```\nuname -a \n```\n\n#### List all server resources for the current user.\n```\nulimit -a \n```\n\n#### Sets the maximum value for the flag.\n```\nulimit -S[flag from ulimit -a] unlimited \n```\nI.e.\n```\n# Sets the maximum number of processes that the user can use.\nulimit -Su unlimited \n```\n\n#### Returns maximum processes for current user.\n```\nulimit -u \n```\n\n#### Num of processes currently running.\nps -e | wc -l \n\n4,194,303 is the maximum limit for Linux x86_64\nand 32,767 is the maximum limit for Linux x86\n\n#### Process columns description - Ref. https://kb.iu.edu/d/afnv\n```\nps -elf\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralphmoran%2Fparalleljobs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fralphmoran%2Fparalleljobs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fralphmoran%2Fparalleljobs/lists"}