php多线程thread开发与应用的例子
php多线程的使用,首先需要PHP5.3以上版本,并安装pthreads PHP扩展,可以使PHP真正的支持多线程,扩展如何安装请自行百度.
PHP扩展下载:https://github.com/krakjoe/pthreads
PHP手册文档:http://php.net/manual/zh/book.pthreads.php
在安装好扩展之后,就可以运用多线程了,下面贴个通过搜索结果抓取百度网盘内容的代码:
<?php include 'include/CurlLoad.class.php'; // 引入读取库 /** * 多线程抓取内容 * @param array $url 待抓取URL列表集合 * @return 成功返回指定内容,失败返回NULL */ function vget($url) { $ret = BaiduSRLinksGet($url, 1); // 获取结果列表地址 if ($ret != null) { if (array_key_exists("links", $ret)) { $infos = array(); $number = count($ret['links']); for ($i = 0; $i < $number; $i++) { //循环创建线程对象 $thread_array[$i] = new baidu_thread_run($ret['links'][$i]); $thread_array[$i]->start(); } foreach ($thread_array as $thread_array_key => $thread_array_value) { //检查线程是否执行结束 while ($thread_array[$thread_array_key]->isRunning()) { usleep(10); } if ($thread_array[$thread_array_key]->join()) { //如果执行结束,取出结果 $temp = $thread_array[$thread_array_key]->data; if ($temp != null) $infos['res'][] = $temp; } } $infos['pages'] = $ret['pages']; $infos['status'] = "1"; } else $infos = null; } else $infos = null; return $infos; } /** * 获取百度搜索结果列表URL * * @param string $url * 搜索结果页URL * @param int $format * 默认$format=0,获取默认地址;$format=1获取跳转后真实地址 * @return NULL multitype:array() */ function BaiduSRLinksGet($url, $format = 0) { $html = CurlLoad::HtmlGet($url); // 获取页面 if ($html == null) return null; try { preg_match_all("/"url":"( ? < links > . *) "}/", $html, $rets); // 搜索结果链接筛选 if (!array_key_exists('links', $rets)) // 如果数组中不包含Links键名,表示获取失败 return null; $ret = array(); if ($format == 1) { $number = count($rets['links']); for ($i = 0; $i < $number; $i++) { $headr_temp = CurlLoad::Get_Headers($rets['links'][$i], 1); // 通过headr获取真实地址 if (array_key_exists("Location", $headr_temp)) $ret['links'][$i] = $headr_temp['Location']; else $ret['links'] = $rets['links']; } } else $ret['links'] = $rets['links']; preg_match_all('/href="?/s?wd=site%3Apan.baidu.com%20(?<url>.+?)&ie=utf-8">/', $html, $out); unset($out['url'][0]); $number = count($out['url']); for ($i = 1; $i < $number; $i++) { preg_match_all('/&pn=(.*)/', $out['url'][$i], $temp); $ret['pages'][$temp[1][0] / 10] = base64_encode($out['url'][$i]); } return $ret; } catch(Exception $e) { WriteLog($e); return null; } } /** * 百度网盘资源信息获取 * * @param string $url * 网盘资源页URL * @return NULL array */ function PanInfoGet($url) { $html = CurlLoad::HtmlGet($url); // 获取页面 if ($html == null) return null; try { if (preg_match_all("/文件名:(?<name>.*) 文件大小:(?<size>.*) 分享者:(?<user>.*) 分享时间:(?<date>.*) 下载次数:(?<number>[0-9]+)/", $html, $ret) == 0) return null; $rets['name'] = $ret['name'][0]; $rets['size'] = $ret['size'][0]; $rets['user'] = $ret['user'][0]; $rets['date'] = $ret['date'][0]; $rets['number'] = $ret['number'][0]; $rets['link'] = $url; return $rets; } catch(Exception $e) { WriteLog($e); return null; } } function WriteLog($str) { $file = fopen("../error.log", "a+"); fwrite($file, "Warning:" . date("Y/m/d H:i:s") . ":" . $str . "rn"); fclose($file); } /** * 多线程抓取对象 * @author MuXi * */ class baidu_thread_run extends Thread { public $url; public $data; public function __construct($url) { $this->url = $url; } public function run() { if (($url = $this->url)) { $this->data = PanInfoGet($url); //线程执行方法 } } } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP does not support multi-threading. The reason is: PHP does not support multi-threading by default. To use multi-threading, you need to install the pthread extension. To install the pthread extension, you must use the --enable-maintainer-zts parameter to recompile PHP.

How to use PHP multi-threading to implement a high-performance RPC server. With the continuous development of the Internet, there are more and more demands for distributed systems. Remote Procedure Call (RPC) is one of the communication mechanisms often used in these distributed systems. It allows programs on different machines to call remote functions just like calling local functions, thereby realizing data transmission and function calls between systems. In actual development, in order to improve the performance and concurrent processing capabilities of the system, multi-threading technology is used to

How to improve database read and write performance through PHP multi-threading. With the rapid development of the Internet, database read and write performance has become a key issue. When our application needs to frequently read and write to the database, using a single-threaded approach often leads to performance bottlenecks. The use of multi-threading can improve the efficiency of database reading and writing, thereby improving overall performance. As a commonly used server-side scripting language, PHP has flexible syntax and powerful database operation capabilities. This article will introduce how to use PHP multi-threading technology to improve

How to improve the speed of large-scale data sorting through PHP multi-threading. With the rapid development of the Internet and the popularity of big data, the demand for processing massive data is also increasing. Among them, for the common problem of data sorting, how to improve the processing speed has become an urgent problem to be solved. In the field of PHP, multi-threading technology is considered an effective solution. This article will introduce how to improve the speed of large-scale data sorting through PHP multi-threading. 1. The principle of multi-threading Multi-threading refers to the existence of multiple threads at the same time. Multiple threads can execute different tasks at the same time.

How to improve database query performance through PHP multi-threading Introduction: With the rapid development of the Internet, database query performance has become one of the important challenges faced by developers. As a widely used server-side scripting language, PHP also plays an important role in database queries. This article will explore how to improve database query performance through PHP multi-threading technology to meet the needs of high concurrent requests. 1. What is multi-threading? Before discussing how to use multi-threading to improve database query performance, we first need to understand what multi-threading is. popular

How to accelerate large file downloads through PHP multi-threading In today's Internet era, file transfer has become more and more common and important. However, for larger files, the download time increases significantly, causing inconvenience to users. In order to improve the download speed of large files, we can achieve acceleration through PHP multi-threading. This article will introduce how to speed up large file downloads through PHP multi-threading. First, in order to implement PHP multi-threaded downloading, we need to do some preparations. Make sure the latest version of PHP is installed on the server and enabled

PHP multi-threaded programming practice: using coroutines to implement concurrent task processing. With the development of Internet applications, the requirements for server performance and concurrent processing capabilities are becoming higher and higher. Traditional multi-threaded programming is not easy to implement in PHP, so in order to improve PHP's concurrent processing capabilities, you can try to use coroutines to implement multi-threaded programming. Coroutine is a lightweight concurrency processing model that can implement concurrent execution of multiple tasks in a single thread. Compared with traditional multi-threading, coroutine switching costs are lower

In PHP programming, multi-threading is a very important programming technique if we need to perform multiple tasks or handle multiple requests at the same time. Multi-threading can enable multiple threads to run at the same time, improving program efficiency and user experience. 1. Introduction to PHP multi-threading PHP multi-threading refers to a program that executes two or more threads at the same time. Each thread is an independent sub-process and can perform tasks independently. In PHP, multithreading can be handled through the pcntl extension. The pcntl extension is a process control extension supported by PHP
