zhgzhg / gphpthread
仅使用纯PHP实现的通用PHP线程库
1.0.6
2024-01-02 19:30 UTC
Requires
- php: >=5.3
- ext-pcntl: *
- ext-posix: *
README
这是一个使用纯PHP编写的重型线程库实现。当宿主系统未安装PHP线程模块,或者由于某些原因无法安装(缺乏权限、遗留系统等)时,可能非常有用。
特性
- 面向对象线程创建和管理理念
- 线程执行控制
- 启动
- 停止
- 加入 - 阻塞或非阻塞模式
- 暂停
- 恢复
- 可中断的睡眠
- 线程优先级和亲缘度控制
- 支持线程退出代码
- 线程间共享数据或锁定用途的临界区
- 可靠的容器
- 更快、不可靠的容器
- 可扩展和可定制
- 在MIT许可下发行
要求/依赖
- PHP版本 5.3+
- PHP shell执行上下文
- PHP pcntl
- PHP POSIX
- 操作系统 Linux系列
如何使用
本质上,您需要扩展GPhpThread类并实现抽象方法run()。以下是一个示例
<?php
require_once 'GPhpThread.php';
class SingingThread extends GPhpThread {
public function run() {
$j = 99;
for ($i = $j; $i > 0; ) {
echo $this->getPid() . " sings:\n";
echo "{$i} bottles of beer on the wall, {$i} bottles of beer.\n";
--$i;
echo "Take one down and pass it around, {$i} bottles of beer on the wall.\n\n";
}
echo "No more bottles of beer on the wall, no more bottles of beer.\n";
echo "Go to the store and buy some more, {$j} bottles of beer on the wall.\n\n";
}
}
echo "PID " . getmypid() . " is the director! Let's sing!\n\n";
sleep(3);
$sharedCriticalSection = null;
$allowThreadExitCodes = false;
$st = new SingingThread($sharedCriticalSection, $allowThreadExitCodes);
$st->start(); // start the GPhpThread
$st->join(); // wait for the generic thread to finish
echo "Director " . getmypid() . " is done!\n";
?>
有关更多信息,请参阅"examples"和"tests"目录中的文件。在zhgzhg.github.io/GPhpThread/可找到HTML文档。
安装
您可以使用composer将库集成到项目中
php composer.phar require zhgzhg/gphpthread:^1.0.6
或者您也可以手动下载GPhpThread.php文件并将其放置在项目目录中。