ThinkPHP5中使用pthreads多线程

​​​做了个爬虫,因为PHP是单线程,所以爬取速度较慢,故使用了pthreads多线程,实现多线程爬取

pthreads扩展下载地址http://windows.php.net/downloads/pecl/releases/pthreads

扩展文档http://docs.php.net/manual/zh/book.pthreads.php

多线程代码

namespace app\api\controller\v1;
use think\Db; //此处的Db类都以失效,试了多种引入方式都不行
use think\Cache; //同理
use think\Controller;
class  Curl  extends \Thread
{
     public $url;
    public $result;
    public function __construct($url) {
        $this->url = $url;
    }
   //线程运行
    public function run() {
        if ($this->url) {
            $this->result = $this->doshu($this->url);

        }
    }
    public function doshu($url){
        return file_get_contents($url); //所需要访问的网址
    }
}

遇到的问题,线程中使用不了数据库,不知道什么原因,知道的大神可以解释一下,我用了访问内部的url来实现数据写入

public function doZhiHu1(){

//多个本地网址
        $urls = array('http://localhost/shopapi/api/v1.index/doZhiHu2',
            'http://localhost/shopapi/api/v1.index/doZhiHu3',
            'http://localhost/shopapi/api/v1.index/doZhiHu4',
            'http://localhost/shopapi/api/v1.index/doZhiHu5',
            'http://localhost/shopapi/api/v1.index/doZhiHu6');
        foreach ($urls as $key=>$url) {
            $workers[$key] = new curl($url); //new一个新的线程
            $workers[$key]->start(); //开始运行
        }
        foreach ($workers as $key=>$worker) {
            while($workers[$key]->isRunning()) { //查看线程的状态
                usleep(100);  
            }
            if ($workers[$key]->join()) { //等待线程执行结束
                var_dump($workers[$key]->result);
            }
        }
    }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
京东jos是京东提供的一种云服务,包括对象存储、CDN、视频转码等功能。如果想在ThinkPHP5使用京东jos,可以按照以下步骤操作: 1. 安装京东jos SDK 可以通过 Composer 安装京东jos SDK: ``` composer require jingdong-union/jd-union-sdk-php ``` 2. 配置应用信息 在 `config` 目录下新建 `jos.php` 文件,填写如下内容: ```php <?php return [ 'app_key' => 'your app key', 'app_secret' => 'your app secret', 'access_token' => 'your access token', 'bucket_name' => 'your bucket name', ]; ``` 其,`app_key` 和 `app_secret` 为京东jos的应用信息,`access_token` 是访问凭证,`bucket_name` 是对象存储的桶名。 3. 使用对象存储功能 ```php <?php namespace app\index\controller; use think\Controller; use Jdcloud\Credentials\Credentials; use Jdcloud\Result; use Jdcloud\Service\OSS\OSSClient; class Index extends Controller { public function index() { // 实例化OSSClient $credentials = new Credentials(config('jos.app_key'), config('jos.app_secret')); $ossClient = new OSSClient([ 'regionId' => 'cn-north-1', 'credentials' => $credentials, ]); // 上传文件 $file = request()->file('image'); $result = $ossClient->putObject([ 'Bucket' => config('jos.bucket_name'), 'Key' => $file->getOriginalName(), 'Body' => fopen($file->getRealPath(), 'r'), ]); // 打印上传结果 dump($result->toArray()); } } ``` 以上代码演示了在 ThinkPHP5 使用京东jos的对象存储功能,实现了上传文件的功能。其他功能的使用方法类似,只需要根据 SDK 提供的 API 进行调用即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值