sanket3dx/phpgo
最新稳定版本:v1.0.1
Composer 安装命令:
pie install sanket3dx/phpgo
包简介
True Go Concurrency for PHP - Native Extension
README 文档
README
phpgo is a native PHP extension written in Go that brings Go's concurrency primitives to PHP. It enables parallel execution using Goroutines, Channels, WaitGroups, and Select, backed by the Go runtime.
Features
- Goroutines:
phpgo\go(callable)spawns a lightness Go thread. - Channels: Buffered and unbuffered channels (
phpgo\channel). - Select: Go-style
selectstatement (phpgo\select). - WaitGroup: Synchronization primitive (
phpgo\WaitGroup).
Requirements
- PHP 8.0+
- Go 1.18+
- GCC / Clang
- ZTS (Zend Thread Safety) PHP build is highly recommended for stability.
Architecture
phpgo operates by loading a Go-compiled shared object (libphpgo.so) into a thin C-based PHP extension (phpgo.so). The Go runtime manages the scheduling of goroutines and channel operations.
Installation
Option 1: Using Pre-compiled Binaries (Fastest)
- Download the
phpgo.sofrom thebuild/folder. - Copy to Extensions: Move
phpgo.soto your PHP extensions directory (e.g.,/usr/lib/php/20210902/). - Enable in PHP: Add
extension=phpgo.soto yourphp.ini. - Restart: Restart your web server (e.g.,
sudo systemctl restart php8.1-fpm).
Option 2: Build from Source
- Clone the repository.
- Build the Go library:
go build -o go-build/libphpgo.a -buildmode=c-archive src/go/lib.go
- Build the PHP extension:
phpize ./configure --enable-phpgo make
- Load it in PHP:
php -d extension=`pwd`/modules/phpgo.so script.php
Usage
Channels & Goroutines
<?php $ch = phpgo\channel(); phpgo\go(function() use ($ch) { echo "Sending from goroutine...\n"; phpgo\send($ch, "Hello form Go!"); }); $msg = phpgo\receive($ch); echo "Received: $msg\n";
Select
<?php $ch1 = phpgo\channel(); $ch2 = phpgo\channel(); // ... spawn producers ... $result = phpgo\select([ phpgo\case_recv($ch1), phpgo\case_recv($ch2), phpgo\case_default(fn() => "Timeout") ]); var_dump($result['value']);
Safety Warnings
- NTS Builds: Running PHP code concurrently (inside
phpgo\go) on Non-Thread-Safe PHP builds is experimental and may cause crashes if global state is accessed. - Resource Management: Channels are Go objects referenced by ID. They are garbage collected by Go when unreachable, but the ID mapping implies they should be closed explicitly or we rely on the map being cleared (TODO).
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-09