php某些接口被调用可能要消耗很长时间,希望异步来解决,所以想到通过多线程的方式来解决。Google到了pThread,安装后查阅了readme.MD中的sample代码。在不join的情况下仍旧是阻塞的。
官方readme.MD中的sample代码,经常尝试发现是阻塞的,那这thread还有意义吗?如何解决,希望异步解决。
// 请把代码文本粘贴到下方(请勿用图片代替代码)
这是官方sample代码
class AsyncOperation extends Thread { public function __construct($arg){ $this->arg = $arg;
} public function run(){ if($this->arg){
sleep(5);
printf("Hello %s\n", $this->arg);
}
}
}
$thread = new AsyncOperation("World1");if($thread->start())
$thread->join();这是我修改后的代码
class AsyncOperation extends Thread { public function __construct($arg){ $this->arg = $arg;
} public function run(){ if($this->arg){
sleep(5); // do something
printf("Hello %s\n", $this->arg);
}
}
}
$thread = new AsyncOperation("World1");
$thread->start();echo 1111;我期望的结果是页面打印完1111就结束, do something在不join的情况下,子线程单独处理,结束后自行关闭。
米琪卡哇伊
守着一只汪
随时随地看视频慕课网APP