Home > Article > Backend Development > Concurrency and Parallelism in PHP
Concurrency and parallelism are essential concepts in modern programming, allowing applications to perform multiple tasks simultaneously, either through interleaved execution (concurrency) or simultaneous execution (parallelism). PHP, primarily known for its synchronous execution model, has evolved to support these paradigms through various techniques.
PHP traditionally follows a synchronous execution model, especially when used with Apache in a typical web server setup. In this model, each HTTP request is handled by a single PHP process. The steps involved in processing a request include:
This model ensures simplicity and ease of understanding but can become inefficient for tasks requiring parallel execution or handling multiple tasks simultaneously.
As web applications became more complex, the need for concurrent and parallel execution in PHP grew. Let's explore the techniques PHP offers to achieve these paradigms.
Synchronous code is the simplest form of execution where tasks are performed one after the other.
echo "Synchronous Code Example:\n"; function synchronousFunction() { for ($i = 0; $i < 3; $i++) { echo "Synchronous Loop Iteration: $i\n"; sleep(1); } } synchronousFunction();
In this example, each iteration of the loop executes sequentially, with a one-second delay between iterations. This approach is straightforward but inefficient for I/O-bound or CPU-intensive tasks that could benefit from parallel execution.
Forking creates a new process (child) that runs concurrently with the original process (parent). This is useful for parallelizing tasks.
echo "\nForking Process Example:\n"; function forkProcess() { $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { echo "Parent Process: PID $pid\n"; pcntl_wait($status); // Protect against Zombie children } else { echo "Child Process: Hello from the child process!\n"; exit(0); } } forkProcess();
In this code, pcntl_fork() creates a child process. The parent and child processes execute concurrently, allowing parallel task execution. The parent process waits for the child process to finish to avoid creating zombie processes.
PHP's threading capabilities are available through extensions like pthreads. Threads are lighter than processes and share the same memory space, making them suitable for tasks requiring shared data.
if (!class_exists('Thread')) { die("Threads are not supported in this PHP build\n"); } echo "\nThreading Example:\n"; class MyThread extends Thread { public function run() { for ($i = 0; $i < 3; $i++) { echo "Thread Loop Iteration: $i\n"; sleep(1); } } } $thread = new MyThread(); $thread->start(); $thread->join();
This example defines a MyThread class extending Thread. The run method is executed in a new thread, running concurrently with the main thread. This approach is useful for I/O-bound operations where threads can handle waiting for resources.
Generators provide a way to implement simple co-routines, allowing functions to yield results iteratively without blocking the entire program.
echo "\nGenerators Example:\n"; function simpleGenerator() { yield 'First'; yield 'Second'; yield 'Third'; } $gen = simpleGenerator(); foreach ($gen as $value) { echo "Generator Yielded: $value\n"; }
Generators use the yield keyword to produce values one at a time, allowing the function to be paused and resumed, facilitating a form of cooperative multitasking.
PHP has come a long way from its synchronous roots to support various forms of concurrency and parallelism. While synchronous code remains simple and effective for many use cases, techniques like forking processes, threading, and using generators open up new possibilities for handling complex, parallelizable tasks efficiently.
The above is the detailed content of Concurrency and Parallelism in PHP. For more information, please follow other related articles on the PHP Chinese website!