定制 sj-i/ffi-zts-parallel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sj-i/ffi-zts-parallel

Composer 安装命令:

composer require sj-i/ffi-zts-parallel

包简介

PHP-side wrapper around pecl/parallel for use through sj-i/ffi-zts (NTS host -> embedded ZTS interpreter -> real OS threads).

README 文档

README

NTS-hosted wrapper around pecl/parallel that runs through sj-i/ffi-zts.

Gives a plain non-thread-safe PHP CLI access to real OS-thread parallelism via parallel\Runtime, without replacing the system PHP build. See docs/DESIGN.md in the core package for the full design.

Install

composer require sj-i/ffi-zts-parallel

Both sj-i/ffi-zts and sj-i/ffi-zts-parallel ship as Composer plugins. On install, each downloads the pre-built binary matching your host's PHP minor / CPU arch / libc:

  • vendor/sj-i/ffi-zts/bin/libphp.so
  • vendor/sj-i/ffi-zts-parallel/bin/extensions/ffi-zts/parallel.so

Trusting the plugins

Composer 2.2+ asks you to trust a new plugin before running it. In interactive shells you get a (y/N) prompt on first install. In CI / non-interactive environments, whitelist both up front:

composer config allow-plugins.sj-i/ffi-zts true
composer config allow-plugins.sj-i/ffi-zts-parallel true
composer require sj-i/ffi-zts-parallel

Manual install / retry

If either plugin was skipped (--no-plugins, network outage, binary not yet published for a new PHP minor), retry the binary fetch on demand from the root project:

vendor/bin/ffi-zts install   # fetches libphp.so
# parallel.so is re-fetched on the next `composer update sj-i/ffi-zts-parallel`.

Usage

<?php
require __DIR__ . '/vendor/autoload.php';

use SjI\FfiZts\Parallel\Parallel;

Parallel::boot()
    ->runScript(__DIR__ . '/worker.php');

A minimal worker.php that fans out four parallel\Runtimes inside the embed:

<?php
$futures = [];
for ($i = 0; $i < 4; $i++) {
    $rt        = new parallel\Runtime();
    $futures[] = $rt->run(function (int $id): array {
        $sum = 0;
        for ($j = 0; $j < 2_000_000; $j++) $sum += $j;
        return ['id' => $id, 'pid' => getmypid(), 'zts' => PHP_ZTS, 'sum' => $sum];
    }, [$i]);
}
foreach ($futures as $f) {
    print_r($f->value());
}

All four workers report the same pid (same process) and zts=true (running in the embedded ZTS interpreter).

opcache.preload (2.x / PHP 8.5)

PHP 8.5 links opcache statically into libphp.so, so opcache.preload works under the embed out of the box and the preloaded classes / functions propagate into every parallel\Runtime worker thread:

Parallel::boot()
    ->withIniEntry('opcache.enable_cli',  '1')
    ->withIniEntry('opcache.preload',     __DIR__ . '/preload.php')
    ->withIniEntry('opcache.preload_user', get_current_user())
    ->runScript(__DIR__ . '/worker.php');

See docs/PERFORMANCE.md in the core repo for fork-vs-embed measurements.

Read-only / containerised environments

If the vendor directory is not writable at runtime (baked into a container image, mounted read-only, etc.), Parallel::bootInMemory() skips the disk cache and patches + loads parallel.so via memfd_create(2):

Parallel::bootInMemory()->runScript('worker.php');

Versioning

Major tracks the host PHP minor:

  • ffi-zts-parallel 1.x targets PHP 8.4 (host NTS / embedded ZTS).
  • ffi-zts-parallel 2.x targets PHP 8.5 and picks up upstream's static opcache for out-of-box opcache.preload.

Minor / patch tracks upstream parallel releases plus wrapper fixes; bumping just the parallel version is composer update sj-i/ffi-zts-parallel.

Requirements

  • Linux x86_64 or aarch64, glibc 2.31+
  • Host NTS PHP matching the major you install -- 1.x needs PHP 8.4, 2.x needs PHP 8.5 -- with ext-ffi enabled
  • Composer 2.2+

macOS, Windows, and musl are out of scope for the currently supported major lines; see the design doc for why.

sj-i/ffi-zts-parallel 适用场景与选型建议

sj-i/ffi-zts-parallel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「threads」 「parallel」 「composer-plugin」 「zts」 「ffi-zts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 sj-i/ffi-zts-parallel 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-20