You can edit almost every page by Creating an account. Otherwise, see the FAQ.

Asynchronous libraries (Multi-Thread & Multi-Process) in PHP

From EverybodyWiki Bios & Wiki




PHP was created to be a request-response example, so initially it was only synchronous and did not have asynchronism in mind, modern period brought a necessity for asynchronism. While being still unavailable in the core of PHP, individual entities created solutions & wrappers built onto PHP to overcome the problem.

Differences between Multi-Process & Multi-thread[edit]

Typically multi-process functionality means that the program works on same (single) thread, but has an ability to create multiple processes in that thread. On the other hand, multi-threading gives an opportunity to a program to align its work on multiple CPU cores. Both of the approaches have their advantages and disadvantages. Concurrency allows program to execute operations on same thread,but switching from one pipeline to another to use the resources in best & optimal way. Parallelism allows true parallel execution of operations (i.e. CPU intensive or I/O too) in separate thread processes.

Parallel VS non-Parallel


Synchronous VS Asynchronous


Depending the amount of I/O tasks you do, you can benefit from multi-process asynchronous functionality. For example, HTTP-requests is a good example (while server waits for response) to take a benefit of asynchronous approach, but if you do CPU computational work (on same thread), then asynchronity will not have any opportunity to benefit from. In such cases, multi-threading can be used.

Multi-process tools[edit]

Exec / pcntl_fork / Proc-open /popen[edit]

`exec, pcntl_fork, proc_open, popen ` (and several others alike) create processes that can run in parallel. That low-level API is decades old, but there are some wrapper libraries built onto them.

Spatie[edit]

Spatie is a wrapper, that offers not just concurrent, but also parallel execution.

ReactPHP[edit]

ReactPHP - another solid library for multi-process functionality.

amphp & amphp parallel[edit]

AmPHP and ReactPHP have a different syntax, but they do somewhat same. Someone might refer them as "Node.js in PHP." They use async IO using Promises, but not true multi-threading or multi-process. Another difference is that ReactPHP doesn't allow to run closures or Tasks as sub-processes the way Amp/Spatie can.

appserver.io[edit]

AppServer.io - a multi-threaded application server built for PHP.

Swoole (comes as extension)[edit]

Swoole is an extension and it uses different model than Amp/React/Fibers. It has many `on` event handlers and promises (like client-side javascript). Depending the needs, in Swoole you can create concurrency with different ways: threads, fibers, coroutines, event loop, etc. Unlike to Go (programming language, which uses multiple concurrently running threads and it switches context between coroutines preemptively), Swoole is single-thread execution at any one time (like PHP & Fibers, Node...) . In JavaScript you can have the endless callbacks firing the next callback asynchronously, however in Swoole callbacks at sent to specific Swoole command to execute those callback asynchronously at just one level, mainly using coroutines and they act a bit like blocks of JavaScript async/await... However, it is not multithreading/parallel execution of functions.

PhpDaemon[edit]

PhpDaemon is being used for high-load servers & workloads. Its workers are capable of handling thousands of simultaneous connections and have specific use cases.

PHP 8 Fibers[edit]

Fibers functionality is introduced in PHP 8.1 and it is a low-level tool. It allows you to have async bits (mainly around IO), while the program does not need to know that it is async (unlike React, AMP, Node.js...). PHP Fibers will be used inside ReactPHP and AmPHP to make them better, so, you'll probably also use those wrapper tools rather than use the API directly.

Multi-threading tools[edit]

kraken[edit]

Kraken is an useful tool allowing both multi-processing and multi-threading, with easy API.

Parallel [pThreads successor][edit]

Parallel is an extension which brings real Unix threads into PHP (note, it is the successor of pThreads extension, which was created by the same author).

amphp parallel[edit]

With amphp & parallel , you can have parallel processing with multi-processing & multi-threading, with non-blocking tools.

Separate PHP thread from parent application[edit]

There is another way to start a PHP process in separate thread from parent application, like Crontab or whatever, which can start new instance of your PHP application in separate thread, however the above described tools help to make the job easier from within PHP application.

*Notes*[edit]

(Some of the libraries allow execution only in CLI and not in APACH/PHP-FPM mode, but with some efforts you can run them in their own process, which will communicate with incoming port queries. )

Useful resources[edit]

  • There exists "multi-curl" functionality too, however multi-curl is just a specific case for concurrent HTTP requests and doesn't fit in this topic as a separate tool. You can find full example of multi-curl usage here.

References[edit]


This article "Asynchronous libraries (Multi-Thread & Multi-Process) in PHP" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Asynchronous libraries (Multi-Thread & Multi-Process) in PHP. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.