承接 byjg/phpthread 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

byjg/phpthread

Composer 安装命令:

composer require 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.

README 文档

README

sidebar_key phpthread
tags
php concurrency

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 Build Status Opensource ByJG GitHub source GitHub license GitHub release

Table of Contents

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

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?

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

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: 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.

Requirements:

  • 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: Truly asynchronous and non-blocking task management with a JavaScript-like Promise API (docs, benchmark).

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

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

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

Requirements

Non-ZTS (Default PHP)

  • PHP ≥8.3
  • pcntl extension
  • shmop extension

ZTS (Zend Thread Safety)

  • PHP ≥8.3 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
Loading

Open source ByJG

byjg/phpthread 适用场景与选型建议

byjg/phpthread 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.94k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 byjg/phpthread 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 byjg/phpthread 我们能提供哪些服务?
定制开发 / 二次开发

基于 byjg/phpthread 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-08