Concurrency in PHP: Threads, Goroutines, and async/await — Analysis with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Should a PHP Developer Think About Concurrency?

\n\n

Once upon a time, it was believed that PHP was a language for "quick scripts," where everything runs sequentially and no serious parallel work is needed. Times have changed. Today, we build high-load APIs, task queues, web sockets, and even microservices with PHP. And here the question arises: how do you make PHP do several things at once without blocking everything?

\n\n

In this article, we will analyze three approaches to concurrency in PHP: classic threads (pthreads), lightweight goroutines (Fiber), and the modern async/await syntax. You will see real code examples, learn which tool to apply where, and what is generally worth choosing in 2025.

\n\n

If you are looking for parallel execution in PHP for data processing, or want to understand how PHP goroutines differ from threads — you have come to the right place.

\n\n

1. Multithreading in PHP: pthreads — Old but Mighty

\n\n

What is it and how does it work?

\n

pthreads is an extension that gives PHP the ability to create real OS threads. Each thread is a separate instance of the Zend Engine with its own memory, but with the ability to exchange data via Threaded objects.

\n\n

An important nuance: pthreads only works in CLI mode and is not supported in a web environment (Apache/Nginx mod_php). However, it is the only way to achieve true parallelism (not just concurrency) at the CPU level.

\n\n

Example: Parallel URL Parsing

\n
<?php\n// Make sure the pthreads extension is installed\nclass UrlFetcher extends Thread {\n    private $url;\n    private $result;\n\n    public function __construct(string $url) {\n        $this->url = $url;\n    }\n\n    public function run() {\n        // Executed in a separate thread\n        $this->result = file_get_contents($this->url);\n    }\n\n    public function getResult() {\n        return $this->result;\n    }\n}\n\n$urls = ['https://api.example.com/1', 'https://api.example.com/2', 'https://api.example.com/3'];\n$threads = [];\n\nforeach ($urls as $url) {\n    $thread = new UrlFetcher($url);\n    $thread->start(); // Start the thread\n    $threads[] = $thread;\n}\n\n// Collect results\nforeach ($threads as $thread) {\n    $thread->join(); // Wait for completion\n    echo $thread->getResult() . PHP_EOL;\n}\n?>
\n\n

The code launches three threads, each fetching its own URL. The execution time is approximately equal to the time of the slowest request, not the sum of all three. An excellent illustration of parallel execution in PHP.

\n\n

When to use pthreads?

\n
    \n
  • Heavy CPU tasks (image processing, encryption, PDF rendering).
  • \n
  • Parsing large volumes of data in CLI scripts.
  • \n
  • When true parallelism is needed, not cooperative multitasking.
  • \n
\n\n

2. Goroutines in PHP: Fiber — Lightweight and Controlled

\n\n

What is Fiber?

\n

Starting with PHP 8.1, the language introduced Fiber — an analogue of goroutines from Go or coroutines from Python. Unlike threads, Fiber does not create a separate OS process. It is a lightweight unit of execution that runs in a single thread and switches at the developer's command.

\n\n

The key idea: you decide when to suspend the current Fiber and pass control to another. This is called cooperative multitasking. It is ideal for I/O operations (API requests, file reading, database operations).

\n\n

Example: Asynchronous Directory Traversal

\n
<?php\nfunction readFileAsync(string $path): Fiber {\n    return new Fiber(function () use ($path) {\n        $handle = fopen($path, 'r');\n        $content = '';\n        while (!feof($handle)) {\n            $chunk = fread($handle, 4096);\n            $content .= $chunk;\n            // Suspend the Fiber to give a chance                

Blogs

Book Recommendations