您好,欢迎访问一九零五行业门户网

php如何实现多线程?

php实现多线程的方法:(推荐学习:php编程从入门到精通)
使用shell_exech函数,以shell的方式,每添加一个线程,就相当于你使用php打开了一个shell进行独立的操作
给你的php添加pthread扩展,然后使用pthread所提供的api来操作php的多线程。
<?phpclass pthreadstest extends thread { public function run () { sleep(5); }}$ts1 = new pthreadstest();$ts1->start(); $ts2 = new pthreadstest();$ts2->start(); ?>
下面是一个线程类,用来请求某一接口。接下来根据它写两个多线程的应用实例:
class request extends thread { public $url; public $response; public function __construct($url) { $this->url = $url; } public function run() { $this->response = file_get_contents($this->url); }}
异步请求
将同步的请求拆分为多个线程异步调用,以提升程序的运行效率。
$chg = new request("www.google.com");$chb = new request("www.baidu.com");$chg ->start();$chb ->start();$chg->join();$chb->join();$gl = $chg->response;$bd = $chb->response;
以上就是php如何实现多线程?的详细内容。
其它类似信息

推荐信息