Download the PHP package mensbeam/fork without Composer
On this page you can find all versions of the php package mensbeam/fork. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mensbeam/fork
More information about mensbeam/fork
Files in mensbeam/fork
Package fork
Short Description Runs code concurrently by forking processes
License MIT
Informations about the package fork
Fork
Fork is a library for running jobs concurrently in PHP. It works by forking the main process into separate tasks using PHP's pcntl
and sockets
extensions. So, it should go without saying that this library will not work on Windows.
There is an existing library for forking processes, spatie/fork. This library on its surface is very similar, but internally it's quite a bit different. Unlike spatie/fork
, mensbeam/fork
does not return an array of returned values from all tasks after all of them have finished. Instead, it uses callbacks to handle output as each task completes. This design prevents potential memory exhaustion when running a large number of tasks, as we encountered when using spatie/fork
. Handling output immediately as tasks finish is more scalable and efficient.
Requirements
- PHP >= 8.1
- ext-pcntl
- ext-sockets
- mensbeam/self-sealing-callable ^1.0
Installation
Install using Composer:
Usage
Here is a simple example. Fork->run()
can accept an array or an \Iterator
of callables to run concurrently and will execute them. This means it can also accept a generator to continuously run tasks concurrently.
Example output:
Callbacks
You can use before()
and after()
to register callbacks to run before or after each task. You can register different callbacks for the parent and child processes.
Concurrency
You can limit how many tasks run concurrently using concurrent()
.
Timeouts
You can set a timeout (in seconds) for each child process:
When a task times out, a TimeoutException
is thrown inside the child process, and a ThrowableContext
object is sent back to the parent.
Stopping tasks
You can stop all currently running and queued tasks from within an after()
callback:
ThrowableContext
When a task throws an exception or error in a child process, a ThrowableContext
instance is returned to the parent.
What it includes
- Error code
- File and line where the throwable was thrown
- Message
- Class type
- Optional stack trace (enabled via
Fork::$tracesInThrowableContexts
) - Any previous throwable chain
Example
Handling traces
You can enable including stack traces in ThrowableContext
objects:
Keep in mind there are some minor limitations, however. Anything that can't be serialized such as Generators, Closures, etc. are all sanitized to strings denoting what they were before being replaced.
Throwing exceptions inside the fork
By default, exceptions inside child processes are caught and sent as ThrowableContext
objects. If you'd like them to be thrown, crash the child process, and therefore be printed (for debugging or crash reporting), set:
Example output:
License
MIT License. See AUTHORS.md for details.
All versions of fork with dependencies
ext-sockets Version *
ext-pcntl Version *
mensbeam/self-sealing-callable Version ^1.0