2017 © Pedro Peláez
 

library phpthread

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

image

byjg/phpthread

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

  • Monday, February 20, 2017
  • by byjg
  • Repository
  • 3 Watchers
  • 5 Stars
  • 8,705 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 11 % Grown

The README.md

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

Build Status Opensource ByJG GitHub source GitHub license GitHub release, (*1)

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., (*2)


Table of Contents

  1. Overview
  2. Why Threads in PHP?
  3. How It Works
  4. Features
  5. Limitations
  6. Installation

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., (*3)


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., (*4)

With PHPThread, you can overcome these limitations by leveraging:, (*5)

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

How It Works

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., (*6)

Requirements:, (*7)

  • pcntl: For process control.
  • shmop: For shared memory.

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., (*8)

Requirements:, (*9)

  • parallel: For multi-threading functionality.
  • shmop: For memory sharing.

Features

  • Thread Management: Simplified thread creation and execution (docs).
  • Thread Pools: Efficiently manage and reuse threads for multiple tasks (docs).
  • Promises (Experimental): Asynchronous task management with a promise-like API (docs).

Supported Promise Methods:, (*10)

  • 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

Read the full list of limitations., (*11)


Installation

Requirements

Non-ZTS (Default PHP)

  • PHP ≥8.1
  • pcntl extension
  • shmop extension

ZTS (Zend Thread Safety)

  • PHP ≥8.1 compiled with --enable-zts
  • parallel extension
  • shmop extension (for Promises support)

Install via Composer

composer require byjg/phpthread

Dependencies

flowchart TD
    byjg/phpthread --> byjg/cache-engine
    byjg/phpthread --> ext-posix
    byjg/phpthread --> ext-pcntl
    byjg/phpthread --> ext-pthreads*

Open source ByJG, (*12)

The Versions

20/02 2017

dev-master

9999999-dev

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

14/02 2017

2.1.0

2.1.0.0

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

02/08 2016

2.0.1

2.0.1.0

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

23/07 2016

2.0.0

2.0.0.0

Polyfill Implementation of Threads in PHP. This class supports both FORK process and native Threads using ZTS compilation.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães

19/11 2015

1.1.1

1.1.1.0

Native Implementation of Threads in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães
by Superuser

06/11 2015

1.1.0

1.1.0.0

Native Implementation of Threads in PHP.

  Sources   Download

MIT

The Requires

 

by João Gilberto Magalhães
by Superuser

27/05 2015

1.0.1

1.0.1.0

Native Implementation of Threads in PHP.

  Sources   Download

The Requires

  • ext-pcntl *

 

by João Gilberto Magalhães
by Superuser

09/01 2015

1.0.0

1.0.0.0

Native Implementation of Threads in PHP.

  Sources   Download

by João Gilberto Magalhães
by Superuser