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 = 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
]);
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.
$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.
#-----------------------------------------------------------------
# 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.
#-----------------------------------------------------------------
# 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.
#-----------------------------------------------------------------
# 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.
#-----------------------------------------------------------------
# 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 created by Thread class
print_r( $thread->getRegisteredThreads() );
#-----------------------------------------------------------------
# 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();
#-----------------------------------------------------------------
# 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();
<?php
# Make 10 async jobs for async_process.php
for( $i = 0; $i < 10; $i++ ){
$id = $i . '_' . time();
# The amp "&" char at the end makes the magic here ;)
`php async_process.php {$id} > /dev/null &`;
}
<?php
# Again, it's important the last amp "&" char to make it asynced and pass this call to Unix/Linux kernel
`php deamon.php > /dev/null &`;
<?php
# List all running processes that contain word in them, including the actual grep process
$instances = `ps -wef | grep "{word}"`;
# Rules out the actual grep process from results
$instances = `ps -wef | grep "{word}" | grep -v grep`;
# Returns ONLY running process with specific columns: PID, ELAPSED TIME, COMMAND, and rules out grep process
$instances = `ps -ro pid,etime,command | grep "{word}" | grep -v grep`;
I.e.
<?php
$instances = `ps -wef | grep "async_process.php"`;
$instances = `ps -wef | grep "async_process.php" | grep -v grep`;
$instances = `ps -ro pid,etime,command | grep "async_process.php" | grep -v grep`;
<?php
$pid_elapsed_time = trim(`ps -p {$pid} -o etime=`);
I.e.
<?php
# It returns 03:07 which is 3 mins and 7 secs
$pid_elapsed_time = trim(`ps -p 37809 -o etime=`);
uname -a
ulimit -a
ulimit -S[flag from ulimit -a] unlimited
I.e.
# Sets the maximum number of processes that the user can use.
ulimit -Su unlimited
ulimit -u
ps -e | wc -l
4,194,303 is the maximum limit for Linux x86_64 and 32,767 is the maximum limit for Linux x86
Process columns description - Ref. https://kb.iu.edu/d/afnv
ps -elf