当前位置: 首页 > news >正文

php fiber 应用

参考

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

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

代码:

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();}}
}
$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 ~");

输出

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 ~"

http://www.xdnf.cn/news/494857.html

相关文章:

  • 【AI生成PPT】使用ChatGPT+Overleaf自动生成学术论文PPT演示文稿
  • NetApp高级磁盘分区(ADP)和常用维护命令介绍
  • Spring Security 集成指南:避免 CORS 跨域问题
  • 精益数据分析(63/126):移情阶段的深度潜入——从用户生活到产品渗透的全链路解析
  • 什么是私有IP地址?如何判断是不是私有ip地址
  • 无需配置光猫,使用网管交换机配合路由器的IPTV功能实现单线复用
  • 前端二进制数据指南:从 ArrayBuffer 到高级流处理
  • Spring AI 本地直接运行 Onnx Embedding 模型,结合 Milvus 实现语义向量的存储和检索
  • 【Linux 学习计划】-- yum
  • 【JavaWeb】MySQL
  • 数据结构day3
  • Flink 数据传输机制
  • 仅需三张照片即可生成沉浸式3D购物体验?谷歌电商3D方案全解析
  • 迁移学习:解锁AI高效学习与泛化能力的密钥
  • AI:人形机器人的应用场景以及商业化落地潜力分析
  • PostgreSQL内幕剖析——结构与架构
  • CSS- 4.2 相对定位(position: relative)
  • 计算机发展的历程
  • turn.js与 PHP 结合使用来实现 PDF 文件的页面切换效果
  • 【React Fiber 架构详解】
  • Android7 Input(七)App与input系统服务建立连接
  • Flask项目实践:构建功能完善的博客系统(含评论与标签功能)
  • 使用Maven部署应用到TongWeb(东方通应用服务器)
  • 我的创作纪念日——《惊变256天》
  • 基于C#的MQTT通信实战:从EMQX搭建到发布订阅全解析
  • OpenResty 深度解析:构建高性能 Web 服务的终极方案
  • C语言_编译全攻略_从原理到实战的深度解析
  • 信息收集+初步漏洞打点
  • 完整卸载 Fabric Manager 的方法
  • JS 高级程序设计 设计模式