ホームページ >バックエンド開発 >PHPチュートリアル >pthreads - 求助,php使用Pthread进行多进程的问题?
以下代码运行没有问题,感觉$urls数组有多少数据,就生产多少进程,如果有几万个数据,是不是会有几万个进程?
如果要设置成只有10个进程来处理$urls数组,应该怎么做?
<code><?php header("Content-Type: text/html;charset=utf-8"); class DuoXianCheng extends Thread { public function __construct($arg){ $this->arg = $arg; } public function run(){ if($this->arg){ // echo $this->result = $this->arg; $this->result = model_http_curl_get($this->arg); } } } $thread = new DuoXianCheng("World"); if($thread->start()){ $thread->join(); } function model_http_curl_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 5); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'); $result = curl_exec($curl); curl_close($curl); return $result; } for ($i = 0; $i $url) { $workers[$key] = new \DuoXianCheng($url); $workers[$key]->start(); } foreach ($workers as $key=>$worker) { while($workers[$key]->isRunning()) { usleep(100); } if ($workers[$key]->join()) { var_dump($workers[$key]->result); } } $e = microtime(true); echo "多线程耗时:".($e-$t)."秒<br>"; /* 单线程速度测试 */ $t = microtime(true); foreach ($urls as $key=>$url) { var_dump(model_http_curl_get($url)); } $e = microtime(true); echo "For循环耗时:".($e-$t)."秒<br>"; ?></code>
以下代码运行没有问题,感觉$urls数组有多少数据,就生产多少进程,如果有几万个数据,是不是会有几万个进程?
如果要设置成只有10个进程来处理$urls数组,应该怎么做?
<code><?php header("Content-Type: text/html;charset=utf-8"); class DuoXianCheng extends Thread { public function __construct($arg){ $this->arg = $arg; } public function run(){ if($this->arg){ // echo $this->result = $this->arg; $this->result = model_http_curl_get($this->arg); } } } $thread = new DuoXianCheng("World"); if($thread->start()){ $thread->join(); } function model_http_curl_get($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 5); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'); $result = curl_exec($curl); curl_close($curl); return $result; } for ($i = 0; $i $url) { $workers[$key] = new \DuoXianCheng($url); $workers[$key]->start(); } foreach ($workers as $key=>$worker) { while($workers[$key]->isRunning()) { usleep(100); } if ($workers[$key]->join()) { var_dump($workers[$key]->result); } } $e = microtime(true); echo "多线程耗时:".($e-$t)."秒<br>"; /* 单线程速度测试 */ $t = microtime(true); foreach ($urls as $key=>$url) { var_dump(model_http_curl_get($url)); } $e = microtime(true); echo "For循环耗时:".($e-$t)."秒<br>"; ?></code>
curl_init()不开新进程,但客户机的file descriptor是有限的,内存也是有限的,意味着能new的Thread不是无限的。你要达到pthread要达到的目的,不妨看看http://php.net/manual/en/function.curl-multi-init.php,它允许你异步的处理多个curl_init()返回。
Thread 类是线程,不是进程。
创建十个 DuoXianCheng 的对象:
for ($i = 0 ; $i $obj[$i] = new DuoXianCheng("World");
$obj[$i]->start();
}
感谢大家的回复!我指的是线程,不是进程,打字打错了!
如果$urls有十万条数据,然后只开十个线程,应该怎么操作?