当前位置: 首页 > 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 ~"

相关文章:

  • OpenCV透视变换
  • Java 应用如何实现 HTTPS:加密数据传输的实用指南
  • Ankr:Web3基础设施的革新者
  • 【第二篇】 初步解析Spring Boot
  • 【Python 算法零基础 3.递推】
  • 防范Java应用中的恶意文件上传:确保服务器的安全性
  • 【Redis】redis用作缓存和分布式锁
  • Docker常见命令解读
  • EXO分布式部署deepseek r1
  • 机器学习 KNN算法
  • 智慧水务关键一环:Profinet转Modbus TCP网关驱动供水系统高效互联
  • 计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM-Attention时间序列预测(完整源码和数据)
  • 【深度学习新浪潮】大模型时代,我们还需要学习传统机器学习么?
  • [system-design] ByteByteGo_Note Summary
  • 大型语言模型中的QKV与多头注意力机制解析
  • gem5-gpu教程 第十章 关于topology 的Mesh network
  • Spring Security 集成指南:避免 CORS 跨域问题
  • Axure制作可视化大屏动态滚动列表教程
  • 【开源Agent框架】Suna架构设计深度解析与应用实践
  • 【EDA软件】【联合Modelsim仿真使用方法】
  • 乌称苏梅州一公共汽车遭俄军袭击,致9死4伤
  • 国防部:中方愿与俄方不断增强两军关系良好发展势头
  • 澎湃·镜相第二届非虚构写作大赛初选入围名单公示
  • 党建评:对违规宴饮等问题要坚决露头就打
  • 多个侵华日军细菌战部队留守名簿文件首次公布
  • 王毅谈中拉论坛第四届部长级会议重要共识