zoukankan      html  css  js  c++  java
  • PHP pthread 多线程 案例

    <?php
    /**
     *检测http服务是否可以正常访问
     *启动三个线程执行任务
     */
    class taskWork extends Thread
    {
        public $url = ''; //检测任务URl
        public $name = '';    
        public $thread_id = '';
        public $is_runing = true;   //运行标志
        public function __construct($name)
        {
            $this->name = $name;
        }
        public function run()
        {
            while($this->is_runing)  
            {
                if(!empty($this->url))  //判断当前进程是否存在任务
                {
                    echo "线程:[{$this->name}]正在处理 URL:[{$this->url}]
    ";
                    $t1 = microtime(true);
                    $httpcode = $this->httpcode($this->url);
                    $t2 = microtime(true);
                    $t = $t2-$t1;
                    if($httpcode == 200)
                    {
                        echo "URL:[{$this->url}] 处理结果 正常 请求时间{$t}
    ";
                    }else{
                        echo "URL:[{$this->url}] 处理结果 异常 请求时间{$t}
    ";
                    }
                    $this->url = '';  //任务执行完 清空
                }else{
                    echo "线程:[{$this->name}]等待任务....
    ";
                }
                sleep(1);
            }
        }
        public function httpcode($url){
    
              $ch = curl_init();
              $timeout = 3;
              curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
              curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
              curl_setopt($ch, CURLOPT_HEADER, 1);
              curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
              curl_setopt($ch,CURLOPT_URL,$url);
              curl_exec($ch);
              $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
              curl_close($ch);
              return $httpcode;
        }
    }
    
    $urls=[
        'https://www.1.com',
        'https://www.2.com',
        'https://www.3.com',
        'https://www.baidu.com',
        'https://www.163.com',
        'https://www.qq.com',
        'https://www.www.sina.com.cn',
        'https://www.51cto.com',
        'https://www.9.com',
        'https://www.0.com',
    ];
    
    $threads[] = new taskWork('thread_1');
    $threads[] = new taskWork('thread_2');
    $threads[] = new taskWork('thread_3');
    /**
      *创建三个线程
      *如果不希望在thread->start()后就运行程序
      *就执行$thread->wait()后线程并不会立即运行
      *收到 $thread->notify()信号后才运行程序
      */
    foreach($threads as $thread)
    {
        $thread->start();
    }
    //线程派发任务
    for($i = 1; $i<10; $i++)
    {
      while (true) {
        foreach ($threads as $worker) {
          if ($worker->url=='') {
            $worker->url = array_pop($urls);  //给每个人线程派发任务
            echo "线程:[{$worker->name}]空闲,放入参数{$worker->url}
    ";
            break 2;
          }
        }
        sleep(1);
      }
    }
    echo "所有线程派发完毕,等待执行完成.
    ";
    
    while (count($threads)) {
      foreach ($threads as $key => $thread) {
        if ($thread->url == '') {
          echo "[{$thread->name}]线程运行完成,空闲 退出.
    ";
          $thread->is_runing = false;  //停止线程运行标志
          unset($threads[$key]);
        }
      }
      echo "等待中其他线程完成...
    ";
      sleep(1);
    }
    
    
     
  • 相关阅读:
    Oracle Cannot Update TOP N Issue, 请专家解答
    .NET 匿名方法的BUG,请专家解答
    那些年我们追过的SQL
    迁移至csdn
    Vuejs的一些总结
    CSS命名规范——BEM思想
    Vuejs的一些总结
    shadow-dom 浅析
    javascript 对象封装的常用方式
    JavaScript的性能优化:加载和执行
  • 原文地址:https://www.cnblogs.com/zz-952/p/9762284.html
Copyright © 2011-2022 走看看