ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
事件循环 ~~~ PHP轮询类库:reactphp PHP轮询类库:icicle.io ~~~ PHP中的轮询 在PHP中,我们可以使用类库Icicle来实现,代码如: ~~~ use Icicle\Loop; Loop\timer(0.1, function() { print "inside timer"; }); print "outside timer"; Loop\run(); ~~~ 或者使用类库React来实现: ~~~ $loop = React\EventLoop\Factory::create(); $loop->addTimer(0.1, function () { print "inside timer"; }); print "outside timer"; $loop->run(); ~~~ 勉强使用php来模拟js中的setTimeout,其代码如下: ~~~ <?php function setTimeout(callable $callback, $delay) { $now = microtime(true); while (true) { if (microtime(true) - $now > $delay) { $callback(); return; } } } setTimeout(function() { print "inside the timeout"; }, 1); print "outside the timeout"; ?> ~~~ 显然,php中的输出顺序是: ~~~ inside the timeout outside the timeout ~~~