Download the PHP package justmisha/php-multirunner without Composer
On this page you can find all versions of the php package justmisha/php-multirunner. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download justmisha/php-multirunner
More information about justmisha/php-multirunner
Files in justmisha/php-multirunner
Package php-multirunner
Short Description The package gives the ability to run multiple processes (programs and scripts) in parallel in the background.
License MIT
Homepage https://github.com/JustMisha/php-multirunner
Informations about the package php-multirunner
PHP MultiRunner
A package for running multiple processes in parallel in the background and, if necessary, get the results of their work.
It is clear that, first of all, such parallel execution allows you to radically reduce the time of all these processes.
This package:
- has a very simple interface;
- can run any program, script or code that has an interpreter installed on the system;
- works on both Windows and Linux;
- allows you to transfer large amounts of data between processes and your code -
tests confirm 1/6 of the value set by the memory_limit parameter in php.ini
with 3 processes, i.e:
- 21Mb with memory_limit=128Mb;
- 42Mb with memory_limit=128Mb.
Under the hood, it uses proc_open() to start processes and hides numerous complexities and peculiarities of working with processes in PHP.
It does not require any other packages or extensions to be installed.
Usage
Run a single PHP script many times with different parameters and get its output messages.
General description of the interface
Six different classes can be used depending on the objects that are launched for execution:
- ProgramMultiRunner to run several instances of one program;
- DiffProgramMultiRunner to run miscellaneous programs;
- ScriptMultiRunner to run several instances of one script;
- DiffScriptMultiRunner to run different scripts (and/or even different interpreters);
- CodeMultiRunner to run any code created in your program;
- DiffCodeMultiRunner to run different code (possibly for different interpreters) created in your program.
The signature of the constructors of all these classes and their addProcess()
method will naturally be different.
In any case, the first parameter of the constructor will always be the number of concurrent parallel processes that can be launched. This number depends entirely on the machine on which the package is running, that's why it is the first parameter and has no default value
Explicit is better than implicit. --- The Zen of Python
A good starting point would be 512 simultaneous parallel processes.
And then there are three possible use cases:
- Run and get the results of all running processes — ;
- Run and forget (do nothing) — ;
- Run and get the first N results — .
Data Exchange Between Parent and Child Processes
The package uses the following data exchange scheme between parent code and child processes:
- One-time data transfer in the form of parameters when a child process is added;
- Receiving data from child processes from their standard output streams (stdout) and error output (stderr).
There is no data exchange between the parent process and running child processes after they are started.
To ensure that data other than plain text is properly transferred from a child process, it must be properly encoded: serialization, json representation, base64 conversion or other means of representing the data.
Limitations
It is not intended to use output redirection when starting processes because.
- It may interfere with the established method of retrieving data from child processes;
- It will not work on Windows because when the proc_open() function is called, the bypass_shell option is set to true and the process is started bypassing the cmd.exe shell.
Dependency inversion during dependency injection
If you need to pass some child class as a parameter to a constructor or other method, it is worth using the interface to remove the dependency on a particular class.
More examples
Run one program many times with different parameters and get its messages about the results of its work
Run one program many times with different parameters and do nothing else (run and forget)
Run a single PHP script many times with different parameters and get its output messages
Run one PHP script many times with different parameters and get only a certain number of messages about the results (do not wait for all running processes to execute)
Run one Python script many times with different parameters and get its output messages
Run one PHP script many times with different parameters and do nothing (run and forget).
Run different scripts with different parameters and get his messages about the results of the work
Run different scripts with different parameters and do nothing (run and forget)
Run the PHP code many times with different parameters and get its output messages
Run the PHP code many times with different parameters and do nothing (run and forget)
Run PHP, python, and node.js code with different parameters and get its output messages
Run PHP, python, and node.js code with different parameters and do nothing (run and forget)
Installation
You will need:
- php (version >=7.4);
- Composer;
- Git - for development.
- PHP instances that use this package, must allow . This function is often disabled in some environments due to security policies via in .
For use in your project
For development
In the local project folder, execute
Testing
Run all tests, including linters and static analyzers, from the root of the project folder:
To run only unit tests:
The package was developed in Windows, so to run the tests in Linux via Docker you can use the command file, which will run the unit tests in Docker containers with php-cli 7.4, 8.0, 8.1, 8.2, 8.3. Or you can run a run tests for each php version using a different file (e.g. ).
The tests use the python and node.js interpreters. If you don't have them installed, then run the tests with the command line option or use the configuration file configuration file.
Safety and Security
Actions performed by processes executing this package may cause damage to the environment in which they are running and/or to user data.
The escaping of arguments that the package does is only to ensure that they are correctly passed to the process, and does not protect against malicious use.
Please be careful about what programs, scripts, or code you run and what arguments you pass to them to make sure they do not cause damage.
Contributing
Please send your suggestions, comments, and pull requests.
For large changes, please open a discussion to discuss your suggestions.
For pull requests, please make appropriate changes to the tests.
Versioning
We use SemVer for versioning.
Authors
- Mikhail Trusov - php-multirunner - php-multirunner
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.