An open API service indexing awesome lists of open source software.

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.

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