Download the PHP package aikus/fork-manager without Composer

On this page you can find all versions of the php package aikus/fork-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package fork-manager

ForkManager is a library for simplifying work with running handlers in parallel processes.

It is assumed that this library will help automate the launch of the same type of tasks in cron or monitor child processes in daemons.

Install

To add a library to your project, run the command:

Fast start

The easiest way to run child processes sequentially is as follows:

As a result, you will see a similar output:

You can run this example yourself by running php examples/one-by-one.php at the root of this library.

In this example, we create a fork manager object by calling new ForkManager(1);. We pass him the limit of simultaneously running child processes. Next, by calling addWorker, we add handlers to the manager's waiting queue. To make a worker out of an arbitrary closure, we wrapped it in a CallableWorker object. To start executing handlers, you need to call dispatch(). This call is synchronous and will return control only after the last worker is completed.

If we want to run two (or more) workers in parallel, then we need to create a manager object with a large limit, for example new ForkManager(2). In this case, the output will be something like this:

You can check for yourself by running the command php examples/parallel-two-worker.php at the root of this library.

If you need to run all the workers at once, you can leave the constructor parameter empty or pass 0.

Workers

The base worker

A worker is an object that is responsible for performing work in a child process. Any worker should implement the interface below.

Methods setUp() and run(ForkResult $forkResult) are executed in a child process. The setUp method is called to prepare the worker in the child process (reopen the database connection, close old file descriptors from the parent process, etc). The run method should perform the main useful function of the process. It receives an object of the ForkResult class, which contains information about the result of the fork call.

The afterFinish method is called in the parent process after the child process completes. Accepts as a parameter the ForkManager object that started and waited for the worker to complete, and the WaitStatus object, which contains available information about the child process - its pid, completion status, and completion flag (it is always true in this call).

You can always implement this interface for your workers, but if you need to implement only the payload without preparing the child process and processing its completion, then there are two simpler ways.

The worker function

The easiest way is to wrap an anonymous function in an object of the CallableWorker class. The function is passed to the constructor of this class. This function will be executed when the run method is called. When calling this function, the 'ForkResult` parameter will be passed.

The worker template

You can also create your own worker class, making it an heir to the WorkerTemplate class. 'WorkerTemplate' is an abstract class that requires the implementation of only the run method.

The returned worker

The library also implements a returnable worker, i.e. a worker that, after completing its child process, spawns the child process again. This is a worker of the ReturnableWorker class, which accepts your worker with a payload as the only parameter.

You can see for yourself how the returned workers work by running the command php examples/re-up-workers.php at the root of this library. The result should be something like this:

Asynchronous operation

In the example from the "Quick Start" section, the dispatch call was used for the manager's work, which blocks execution until the last child process completes. Which is suitable for most use cases. The library provides the possibility of asynchronous operation. See the example.

The manager's isEmpty method returns false if the manager has more tasks to complete or there are incomplete child processes. true - if there is neither one nor the other. The asyncTick method performs the minimum unit of the manager's work - it polls all existing processes for completion and calls 'afterFinish` for each completed one. Then, if possible, it launches new workers from the waiting queue.

You can see how the library works in the asynchronous version by making calls:


All versions of fork-manager with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-pcntl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package aikus/fork-manager contains the following files

Loading the files please wait ....