https://github.com/ralphmoran/paralleljobs
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.
https://github.com/ralphmoran/paralleljobs
asynchronous jobs linux multitasking parallel php unix
Last synced: 3 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/ralphmoran/paralleljobs
- Owner: ralphmoran
- Created: 2021-02-01T18:43:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-30T19:00:33.000Z (almost 5 years ago)
- Last Synced: 2023-09-08T00:13:03.194Z (over 2 years ago)
- Topics: asynchronous, jobs, linux, multitasking, parallel, php, unix
- Language: PHP
- Homepage:
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Parallel jobs - Asynced processes - Threads
Vanilla #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.
The 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.
## Thread class
### Instantiation
```php
$thread = new Thread(); # Default thread group is assigned
or
$thread = new Thread([
'group' => 'spx', # 'spx' is a general group label
'execs' => [] # It overwrites, when it's not empty, the default execs. By default, there is just one: php
]);
```
### Dispatching a single thread (default group)
It 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.
```php
$thread->dispatch( 'async_process.php single_command_default_group' );
```
### Dispatching a list of threads, assigning a group name to this colection, registering and dispatching them all.
```php
#-----------------------------------------------------------------
# Register threads, assing a group "g1", and dispatch them all.
$thread
->group( 'g1' ) # 'g1' overwrites general group label (highest priority)
->register(
[
['async_process.php 1 2 3 4 5'],
['async_process.php 1 2 3'],
['async_process.php 1'],
]
);
->dispatchAll(); # Calling dispatch() or dispatchAll() will blank group name.
```
```php
#-----------------------------------------------------------------
# Register a single thread, assing it to a group "g2", and dispatch it.
$thread
->group( 'g2' )
->register( 'async_process.php register_dispatchAll' );
->dispatchAll(); # Calling dispatch() or dispatchAll() will blank group name.
```
```php
#-----------------------------------------------------------------
# Add to a group "g3", register them, and dispatch a collection of commands.
$thread
->group( 'g3' )
->dispatch(
[
['async_process.php d1'],
['async_process.php d2'],
['async_process.php d3'],
]
); # Calling dispatch() or dispatchAll() will blank group name.
```
```php
#-----------------------------------------------------------------
# Dispatch a single command in series, it takes the last assigned group, in this case "g3".
for( $i=0; $i<=2; $i++ )
$thread->dispatch( 'async_process.php single_command_last_group' . $i ); # Calling dispatch() or dispatchAll() will blank group name.
$thread->dispatchAll(); # Calling dispatch() or dispatchAll() will blank group name
```
### Get all registered threads
```php
#-----------------------------------------------------------------
# Get all registered threads created by Thread class
print_r( $thread->getRegisteredThreads() );
```
### Get all running threads or by group or word
```php
#-----------------------------------------------------------------
# Get ONLY running threads by group or word created by Thread class
$thread->getRunningThreads('spx');
$thread->getRunningThreads('g1');
$thread->getRunningThreads('g2');
$thread->getRunningThreads('g3');
# Get ONLY all running threads created by Thread class
$thread->getRunningThreads();
```
## Killing all threads or by group, word
```php
#-----------------------------------------------------------------
# Kill threads by groups or word created by Thread class
$thread->killAll('spx');
$thread->killAll('g1');
$thread->killAll('g2');
$thread->killAll('g3');
# Kill all threads created by Thread class
$thread->killAll();
```
## Important notes for me
### How to make async instances of a PHP script
```php
/dev/null &`;
}
```
## How to wake the deamon up
```php
/dev/null &`;
```
### Other userful commands
#### Get all running Unix/Linux instances that contain "word"
```php