Download the PHP package tbessenreither/quickfork without Composer
On this page you can find all versions of the php package tbessenreither/quickfork. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tbessenreither/quickfork
More information about tbessenreither/quickfork
Files in tbessenreither/quickfork
Package quickfork
Short Description Lightweight php PCNTL wrapper with IPC plus threadded runner with worker pool
License MIT
Informations about the package quickfork
Quickfork
This is a lightweight wrapper for PCNTL that adds Inter Process Communication (IPC) via Unix Sockets and a simple API for running tasks in parallel. It is designed to be easy to use and to integrate into existing projects.
Features
- Run tasks in parallel using a fixed pool of worker processes.
- IPC via Unix Sockets for communication between the main process and worker processes.
- Simple API for defining tasks and handling results.
- Error handling and timeouts for worker processes.
- Tasks are queued and executed as soon as a worker process is available, allowing for efficient resource management.
Installation
You can install Quickfork via Composer.
Usage
Threadded Tasks with worker pool
The main use case of Quickfork is to run Tasks in parallel with a fixed pool of worker processes.
This example command will create 4 tasks that will be run on a maximum of 5 worker threads. Each task will execute the workerThread method, which simulates some work and returns a string. One of the tasks will throw an exception to demonstrate error handling.
If there are more threads than tasks only one thread per task will be created, so in this example only 4 threads will be created. If you have more tasks than threads, the tasks will be queued and executed as soon as a thread is available.
This will print something like this:
Running a Task with bidirectional IPC
This is a more niche use case, but it shows how to setup and use the IPC feature of Quickfork.
We create a single task that will run the taskHandler method. Then the following steps will happen:
- The Quickfork process will trigger the
fork_startmessage, which will be printed out by the parent process. - The
taskHandlerwill send a message with the topicready_for_taskto the parent process. - The
parent processwill receive theready_for_taskmessage and send a new message with the topicnew_taskand some random numbers to sum up. - The
taskHandlerwill receive the message, calculate the sum and then- send a message with the topic
thread_resultback to the parent process. - The
taskHandlerwill then send another message with the topicready_for_taskto indicate that it is ready for another task.
- send a message with the topic
- The
parent processwill receive the result and print it out. - The steps 3-5 will repeat until the parent process has sent out 5 tasks, then it will send a message with the topic
shutdownto the worker thread. - The
taskHandlerwill receive theshutdownmessage and exit the loop.- This will cause Quickfork to trigger the
fork_output,fork_resultandfork_completemessages, which will be printed out by the parent process.
- This will cause Quickfork to trigger the
This will print something like this: