Download the PHP package hds-solutions/parallel-sdk without Composer
On this page you can find all versions of the php package hds-solutions/parallel-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hds-solutions/parallel-sdk
More information about hds-solutions/parallel-sdk
Files in hds-solutions/parallel-sdk
Package parallel-sdk
Short Description SDK to implement parallel php extension
License GPL-3.0
Informations about the package parallel-sdk
Parallel SDK
An implementation of krakjoe/parallel PHP extension.
This library is designed to work even if the parallel
extension isn't available. In that case, the tasks will be executed un sequential order.
That allow that your code can be deployed in any environment, and if parallel
is enabled you will get the advantage of parallel processing.
Installation
Dependencies
You need these dependencies to execute tasks in parallel.
- PHP >= 8.0 with ZTS enabled
- parallel PECL extension (v1.2.5 or higher)
Parallel extension documentation can be found on https://php.net/parallel.
Through composer
Usage
You should set the bootstrap file for the parallel threads. Setting the composer's autoloader is enough.
Behind the scenes, the parallel extension creates an empty Runtime (thread) where the tasks are executed. Every Runtime is a clean, empty, isolated environment without any preloaded classes, functions, or autoloaders from the parent thread/process. This isolation ensures that each runtime starts with a minimal footprint. See references #2 for more info.
Then you define a Worker
that will process the tasks. There are two options:
- Using an anonymous function as a
Worker
. - Creating a class that extends from
ParallelWorker
and implements theprocess()
method.
Then you can schedule tasks to run in parallel using Scheduler::runTask()
method.
Bootstrap a Laravel app
Since ZTS is only available on the cli, you should set the bootstrap file for parallel threads in the artisan
file.
Then, in the bootstrap file for the parallel threads, you just need to get an instance of the app and bootstrap the Laravel kernel. This way you will have all Laravel service providers registered.
bootstrap/parallel.php
:
Anonymous worker
Defining an anonymous function as a Worker
to process the tasks.
Worker instance
Creating a class that extends from ParallelWorker
class. This could be useful for complex processes and to maintain your code clean.
ExampleWorker.php
:
You can also send parameters to the Worker's constructor.
Schedule tasks
After defining a Worker, you can schedule tasks that will run in parallel.
Check Tasks state
Every task has a state. There is also helper functions to check current Task state:
Wait for tasks completion
Instead of checking every task state, you can wait for all tasks to be processed before continue your code execution.
You can also specify a time limit for waiting. The process will pause until all tasks are processed or until max time has been reached, whatever comes first.
Get processed tasks result
Remove pending tasks
You can stop processing queued tasks if your process needs to stop earlier.
Remove a pending/running task
You can remove a specific task from the processing queue if you need to.
Stop processing all tasks immediately
If you need to stop all right away, you can call the Scheduler::stop()
method. This will stop processing all tasks immediately.
Specifying the No. of CPU Cores
You can control the maximum percentage or number of CPU cores to use by calling the following methods:
ProgressBar
Requirements
symfony/console
package- Enable a ProgressBar for the worker calling the
withProgress()
method.
Usage from Worker
Available methods are:
setMessage(string $message)
advance(int $steps)
setProgress(int $step)
display()
clear()
Example output
References
Security Vulnerabilities
If you encounter any security-related issues, please feel free to raise a ticket on the issue tracker.
Contributing
Contributions are welcome! If you find any issues or would like to add new features or improvements, please feel free to submit a pull request.
Contributors
Licence
This library is open-source software licensed under the MIT License. Please see the License File for more information.