PHPackages                             byjg/phpthread - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. byjg/phpthread

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

byjg/phpthread
==============

ByJG PHPThread simplifies working with threads and non-blocking code in PHP, bridging the gap for a language that was not inherently designed for threading.

6.0.0(5mo ago)726.9k↓75%[1 issues](https://github.com/byjg/php-phpthread/issues)MITPHPPHP &gt;=8.3 &lt;8.6CI passing

Since Jan 9Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/byjg/php-phpthread)[ Packagist](https://packagist.org/packages/byjg/phpthread)[ RSS](/packages/byjg-phpthread/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (6)Versions (14)Used By (0)

   sidebar\_key phpthread   tags    php

 concurrency

    ByJG PHPThread: Simplified Threads and Non-Blocking Code in PHP
===============================================================

[](#byjg-phpthread-simplified-threads-and-non-blocking-code-in-php)

ByJG PHPThread simplifies working with threads and non-blocking code in PHP, bridging the gap for a language that was not inherently designed for threading.

[![Sponsor](https://camo.githubusercontent.com/fab14b7f7f475072ada0473f193d6f322561fd4a2958e0cc89910d053347cf27/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d2532336561346161613f6c6f676f3d67697468756273706f6e736f7273266c6f676f436f6c6f723d7768697465266c6162656c436f6c6f723d306431313137)](https://github.com/sponsors/byjg)[![Build Status](https://github.com/byjg/php-phpthread/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-phpthread/actions/workflows/phpunit.yml)[![Opensource ByJG](https://camo.githubusercontent.com/425c1bbccc0f292bf4d20569ae74a6b2e384fd648f1af8911bc61de9a8dcfc0b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f70656e736f757263652d62796a672d737563636573732e737667)](http://opensource.byjg.com)[![GitHub source](https://camo.githubusercontent.com/88e61eb211719144efdd570290a0456b6e13099c2df8d973f1bb43fe33bf0039/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4769746875622d736f757263652d696e666f726d6174696f6e616c3f6c6f676f3d676974687562)](https://github.com/byjg/php-phpthread/)[![GitHub license](https://camo.githubusercontent.com/b6a0d93038dfe60236b93dd1da54abcafecd657d54722569bcc2042f83d14e09/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f62796a672f7068702d7068707468726561642e737667)](https://opensource.byjg.com/opensource/licensing.html)[![GitHub release](https://camo.githubusercontent.com/80eb93e62a340896ff8fa141f50be16a9237e807fff176c372afda21c79e116f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62796a672f7068702d7068707468726561642e737667)](https://github.com/byjg/php-phpthread/releases/)

---

Table of Contents
-----------------

[](#table-of-contents)

1. [Overview](#overview)
2. [Why Threads in PHP?](#why-threads-in-php)
3. [How It Works](#how-it-works)
    - [Default PHP (Non-ZTS)](#default-php-non-zts)
    - [PHP ZTS (Zend Thread Safety)](#php-zts-zend-thread-safety)
4. [Features](#features)
5. [Limitations](#limitations)
6. [Installation](#installation)
7. [Dependencies](#dependencies)

---

Overview
--------

[](#overview)

PHPThread is a polyfill library that abstracts threading functionality for PHP, providing a consistent interface regardless of the underlying PHP setup (ZTS or Fork). It empowers developers to implement thread-like behavior and asynchronous processing in PHP applications.

---

Why Threads in PHP?
-------------------

[](#why-threads-in-php)

PHP is traditionally designed for a **request-response cycle**, where scripts are executed only in response to client requests and terminate after sending a response. While efficient for web applications, this architecture lacks native threading support for background or concurrent tasks.

With PHPThread, you can overcome these limitations by leveraging:

- **Forking (Default PHP)** for simulating threading.
- **Zend Thread Safety (ZTS)** for true multi-threading environments.

---

How It Works
------------

[](#how-it-works)

### Default PHP (Non-ZTS)

[](#default-php-non-zts)

In standard PHP installations (without ZTS), threading is simulated using the `fork` command. This approach creates a new process by cloning the parent process. While not a true thread, this method can approximate threading behavior.

**Requirements:**

- [pcntl](https://www.php.net/manual/en/book.pcntl.php): For process control.
- [shmop](https://www.php.net/manual/en/book.shmop.php): For shared memory.

---

### PHP ZTS (Zend Thread Safety)

[](#php-zts-zend-thread-safety)

With ZTS-enabled PHP, true multi-threading becomes possible. This setup is ideal for production environments where robust threading is required. The ZTS version is compiled with the `--enable-zts` flag, but it may not be included in all PHP distributions.

**Requirements:**

- [parallel](https://www.php.net/manual/en/book.parallel.php): For multi-threading functionality.
- [shmop](https://www.php.net/manual/en/book.shmop.php): For memory sharing.

---

Features
--------

[](#features)

- **Thread Management**: Simplified thread creation and execution ([docs](docs/thread.md)).
- **Thread Pools**: Efficiently manage and reuse threads for multiple tasks ([docs](docs/threadpool.md)).
- **Promises**: Truly asynchronous and non-blocking task management with a JavaScript-like Promise API ([docs](docs/promises.md), [benchmark](docs/promises-benchmark.md)).

Supported Promise Methods:

- `then()`: Execute a callback on promise resolution.
- `catch()`: Execute a callback on promise rejection.
- `finally()`: Execute a callback after resolution or rejection.
- `Promise::resolve()`: Resolve a promise.
- `Promise::reject()`: Reject a promise.
- `Promise::all()`: Wait for all promises to resolve.
- `Promise::race()`: Wait for the first promise to resolve.

---

Limitations
-----------

[](#limitations)

### CLI Mode Only

[](#cli-mode-only)

:::caution Important This library **only works in CLI mode** (command-line interface). It **will NOT work** in web server environments such as:

- PHP-FPM
- Apache with mod\_php
- Any other web server SAPI :::

**Why?**

Both threading approaches used by this library require direct process control, which is unavailable in web server environments:

- **pcntl extension** (used for forking): Disabled in web server contexts for security and stability reasons. The `pcntl_fork()` function and related process control functions only work in CLI mode.
- **parallel extension** (used for true threading): Also requires CLI mode and is not available in web server SAPIs.

Additionally, forking or creating threads within a web server process would interfere with the server's own process/thread management, potentially causing instability.

**Use Cases:**

This library is designed for:

- CLI scripts and daemons
- Background job processors
- Command-line tools
- Long-running CLI applications
- Scheduled tasks (cron jobs)

For web applications requiring asynchronous processing, consider using message queues (RabbitMQ, Redis, etc.) with background workers running in CLI mode.

### Forking and Data Sharing

[](#forking-and-data-sharing)

When simulating threads with `fork`, data cannot be returned directly from the child process to the parent. Use the `shmop` extension to share memory between processes.

However:

- **Avoid returning large or complex objects**, as this may cause memory exhaustion.

---

Installation
------------

[](#installation)

### Requirements

[](#requirements)

#### Non-ZTS (Default PHP)

[](#non-zts-default-php)

- PHP ≥8.3
- `pcntl` extension
- `shmop` extension

#### ZTS (Zend Thread Safety)

[](#zts-zend-thread-safety)

- PHP ≥8.3 compiled with `--enable-zts`
- `parallel` extension
- `shmop` extension (for Promises support)

### Install via Composer

[](#install-via-composer)

```
composer require byjg/phpthread
```

---

Dependencies
------------

[](#dependencies)

 ```
flowchart TD
    byjg/phpthread --> byjg/cache-engine
```

      Loading ---

[Open source ByJG](http://opensource.byjg.com)

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance76

Regular maintenance activity

Popularity26

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity88

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~331 days

Recently: every ~337 days

Total

13

Last Release

174d ago

Major Versions

1.1.1 → 2.0.02016-07-23

2.3.0 → 5.0.02024-12-24

4.9.x-dev → 6.0.02025-11-25

PHP version history (4 changes)2.0.1PHP &gt;5.2.7

2.1.0PHP &gt;5.4

5.0.0PHP &gt;=8.1 &lt;8.4

6.0.0PHP &gt;=8.3 &lt;8.6

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/981924?v=4)[Joao Gilberto Magalhaes](/maintainers/byjg)[@byjg](https://github.com/byjg)

---

Top Contributors

[![byjg](https://avatars.githubusercontent.com/u/981924?v=4)](https://github.com/byjg "byjg (124 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/byjg-phpthread/health.svg)

```
[![Health](https://phpackages.com/badges/byjg-phpthread/health.svg)](https://phpackages.com/packages/byjg-phpthread)
```

###  Alternatives

[ejsmont-artur/php-circuit-breaker

PHP Circuit Breaker component

169964.9k4](/packages/ejsmont-artur-php-circuit-breaker)[topthink/think-helper

The ThinkPHP6 Helper Package

962.4M241](/packages/topthink-think-helper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
