php fiber 应用

参考

基于 PHP Fiber(纤程)的游戏开发分析-腾讯云开发者社区-腾讯云PHP 8.1 引入的 Fibers 为游戏开发带来新机遇,能管理渲染、物理计算等任务且不阻塞主线程。它支持并发,提升效率,简单易用,但也有局限,如单线程本质、上下文切换开销、调试复杂及生态系统不成熟。https://cloud.tencent.com/developer/article/2509749说明

  • 多个用户参与游戏
  • 每个人猜各自的最终数字
  • 每人做多猜五次

代码:

php 复制代码
function getroundnum() {
    $num = rand(0, 30);
    return $num;
}
class Persion {
    public function __construct(public string $name) {

    }
}
class GuessNum extends Persion {
    private int $resultnum;
    public bool $result;
    private int $num;
    private int $type;
    public function __construct(public string $name, public int $gap = 1) {
        $this->num = getroundnum();
        $this->resultnum = getroundnum();
        $this->result = false;
    }
    //type 1加数字 2减数字
    private function getnextnum() {
        if ($this->num === 0) {
            return $this->num;
        }
        $this->num = match ($this->type) {
            1 => $this->num + $this->gap,
            2 => $this->num - $this->gap,
        };
    }
    public function doguessonce() {
        var_dump("resultnum:" . $this->resultnum . " name:" . $this->name . " num:" . $this->num);
        $result = false;
        $this->type = 0;
        match (true) {
            $this->num == $this->resultnum => $result = true,
            $this->num > $this->resultnum => $this->type = 2, //减
            $this->num < $this->resultnum => $this->type = 1//加
        };
        $this->result = $result;
        if (!$result) {
            $this->getnextnum();
        }
    }
}
php 复制代码
$persion_list = [
    new GuessNum("test1", 1),
    new GuessNum("npc", 2),
];

$fibers = [];

foreach ($persion_list as $entity) {
    $fibers[] = new Fiber(function () use ($entity) {
        while (true) {
            $entity->doguessonce($entity);
            Fiber::suspend($entity);
        }
    });
}

// Start all fibers
$endresult = false;
foreach ($fibers as $fiber) {
    $value = $fiber->start();
    if ($value->result) {
        $endresult = true;
        var_dump("game end success name:" . $value->name);
        break;
    }
}
if (!$endresult) {
    for ($i = 0; $i < 5; $i++) {
        foreach ($fibers as $fiber) {
            $value = $fiber->resume();
            if ($value->result) {
                $endresult = true;
                var_dump("game end success name:" . $value->name);
                break;
            }
        }
        sleep(1);
        var_dump("once end");
    }
}
var_dump("game end ~");

输出

php 复制代码
string(30) "resultnum:12 name:test1 num:22"
string(27) "resultnum:7 name:npc num:17"
string(30) "resultnum:12 name:test1 num:21"
string(27) "resultnum:7 name:npc num:15"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:20"
string(27) "resultnum:7 name:npc num:13"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:19"
string(27) "resultnum:7 name:npc num:11"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:18"
string(26) "resultnum:7 name:npc num:9"
string(8) "once end"
string(30) "resultnum:12 name:test1 num:17"
string(26) "resultnum:7 name:npc num:7"
string(25) "game end success name:npc"
string(8) "once end"
string(10) "game end ~"
相关推荐
Algebraaaaa31 分钟前
为什么C++主函数 main 要写成 int 返回值 | main(int argc, char* argv[]) 这种写法是什么意思?
开发语言·c++
java1234_小锋1 小时前
一周学会Matplotlib3 Python 数据可视化-绘制饼状图(Pie)
开发语言·python·信息可视化
悟能不能悟2 小时前
能刷java题的网站
java·开发语言
IT古董3 小时前
【第四章:大模型(LLM)】05.LLM实战: 实现GPT2-(6)贪婪编码,temperature及tok原理及实现
android·开发语言·kotlin
程序员陆通3 小时前
Java高并发场景下的缓存穿透问题定位与解决方案
java·开发语言·缓存
澡点睡觉4 小时前
golang的继承
开发语言·后端·golang
洛阳泰山5 小时前
基于 Easy Rules 的电商订单智能决策系统:构建可扩展的业务规则引擎实践
java·开发语言·规则引擎·easy rules
kushu76 小时前
Java 包
java·开发语言
xiaobobo33306 小时前
C语言中关于普通变量和指针变量、结构体包含子结构体或包含结构体指针的一些思考
c语言·开发语言·结构体指针
java1234_小锋7 小时前
周学会Matplotlib3 Python 数据可视化-绘制折线图(Lines)
开发语言·python·信息可视化·matplotlib·折线图·matplotlib3