Process
in package
Manages processes for multithreading.
Attention: For proper functionality, ensure the presence of php_cli, memcached, and permissions to execute scripts using shell_exec and exec. Before use, verify the existence of the php_cli handler in the folder specified by the Handler constant. If your handler is elsewhere, modify this variable accordingly.
Note: Two instances of the Worker class will be present, one in the source thread and the other in the spawned thread.
Example Usage:
class TestWorker extends Worker { // Wrapper class for the worker
public function Run(): void { // Defines the function and specifies the actions
for ($i = 0; $i < 10; $i++)
$this->_log->debug('Test Worker run ok...', $i, rout($this->_params));
}
}
$worker = new TestWorker();
$process = new Process($worker); // or Process::Create($worker);
$process->Run((object)['blablabla' => 'test']); // Starts the worker and passes parameters to the thread
$workerOutput = [];
$worker->log->Open(); // Opens the worker's log
while ($process->IsRunning()) { // Checks if the worker is still running
$workerOutput = array_merge($workerOutput, $worker->log->Read()); // Reads the latest messages
sleep(1);
}
$worker->log->Close(); // Closes the worker's log
Table of Contents
Properties
- $command : string
- $name : string
- $params : array<string|int, mixed>|object
- $pid : int
- $request : string
- $results : object|null
- $worker : Worker
- $_debug : bool
- Indicates whether to display the command used to start the worker.
- $_entry : string
- Entry point for the console stream.
- $_handler : string
- Request handler, usually php_cli.
- $_params : object
- Parameters for the process.
- $_pid : int
- Process ID (PID) of the worker process.
- $_worker : Worker
- Worker instance to be launched.
- $_workerKey : string
- Key for the worker.
Methods
- __construct() : mixed
- Initializes a new instance of the Process class.
- __get() : mixed
- Magic getter method.
- __set() : void
- Magic setter method.
- ByWorkerName() : Process|null
- Retrieves a Process instance by worker name.
- Create() : Process
- Creates a new instance of the Process class.
- GetWorkerResults() : object|null
- Retrieves the results obtained from the worker.
- IsProcessRunning() : bool
- Checks if a process is running by its PID.
- IsRunning() : bool
- Checks if the worker process is running.
- Run() : void
- Starts the worker process.
- SetHandler() : void
- Sets the handler for processing requests.
- Stop() : bool
- Stops the worker process.
- StopProcess() : bool
- Kills a process by its PID and returns true if successful, false if not.
Properties
$command read-only
public
string
$command
Command used to start the worker
$name read-only
public
string
$name
Name of the process
$params
public
array<string|int, mixed>|object
$params
Parameters passed to the process
$pid read-only
public
int
$pid
Process ID (PID)
$request read-only
public
string
$request
HTTP request URL for the worker
$results read-only
public
object|null
$results
Results obtained from the worker
$worker read-only
public
Worker
$worker
Instance of the Worker class
$_debug
Indicates whether to display the command used to start the worker.
private
bool
$_debug
$_entry
Entry point for the console stream.
private
string
$_entry
$_handler
Request handler, usually php_cli.
private
string
$_handler
= '/usr/bin/php'
Modify this variable if php_cli is located elsewhere on the server.
$_params
Parameters for the process.
private
object
$_params
$_pid
Process ID (PID) of the worker process.
private
int
$_pid
$_worker
Worker instance to be launched.
private
Worker
$_worker
$_workerKey
Key for the worker.
private
string
$_workerKey
= ''
Methods
__construct()
Initializes a new instance of the Process class.
public
__construct(Worker $worker[, bool $debug = false ][, string $entry = '' ][, int $pid = 0 ]) : mixed
Parameters
- $worker : Worker
-
Worker instance to be launched
- $debug : bool = false
-
Indicates whether to display the command used to start the worker
- $entry : string = ''
-
Entry point for the console stream
- $pid : int = 0
-
Process ID (PID) of the worker process
__get()
Magic getter method.
public
__get(string $prop) : mixed
Parameters
- $prop : string
-
Property name
Return values
mixed —Property value
__set()
Magic setter method.
public
__set(string $property, mixed $value) : void
Parameters
- $property : string
-
Property name
- $value : mixed
-
Property value
ByWorkerName()
Retrieves a Process instance by worker name.
public
static ByWorkerName(string $workerName[, bool $debug = false ][, string $entry = '' ]) : Process|null
Parameters
- $workerName : string
-
Name of the worker
- $debug : bool = false
-
Indicates whether to display the command used to start the worker
- $entry : string = ''
-
Entry point for the console stream
Return values
Process|nullCreate()
Creates a new instance of the Process class.
public
static Create(Worker $worker[, bool $debug = false ][, string $entry = '' ]) : Process
Parameters
- $worker : Worker
-
Worker instance to be launched
- $debug : bool = false
-
Indicates whether to display the command used to start the worker
- $entry : string = ''
-
Entry point for the console stream
Return values
ProcessGetWorkerResults()
Retrieves the results obtained from the worker.
public
GetWorkerResults([bool $removeResults = true ]) : object|null
Parameters
- $removeResults : bool = true
-
Indicates whether to remove the results after retrieval
Return values
object|null —Results obtained from the worker
IsProcessRunning()
Checks if a process is running by its PID.
public
static IsProcessRunning(int $pid) : bool
Parameters
- $pid : int
-
Process ID (PID)
Return values
bool —true if running, false if not
IsRunning()
Checks if the worker process is running.
public
IsRunning() : bool
Return values
bool —true if running, false if not
Run()
Starts the worker process.
public
Run([object|null $params = null ]) : void
Parameters
- $params : object|null = null
-
Parameters to pass to the process
SetHandler()
Sets the handler for processing requests.
public
SetHandler(string $handler) : void
Parameters
- $handler : string
-
Request handler
Stop()
Stops the worker process.
public
Stop() : bool
Return values
bool —true if stopped successfully, false if not
StopProcess()
Kills a process by its PID and returns true if successful, false if not.
public
static StopProcess(int $pid) : bool
Parameters
- $pid : int
-
Process ID (PID)
Return values
bool —true if killed, false if not