adolfjap

php 多进程


class vote extends Thread {


    public $content_id  = 0;

    public $runing = false;

    public $isFinished = false;

    public $writer;

    public function __construct($content_id,$writer) {


        

        $this->runing = true;

        $this->content_id = $content_id;

        $this->writer = $writer;

    }


    public function run() 

    {

          // while ($this->runing) {


          //     if ($this->param != 0) {

          //         $nt          = rand(1, 10);

          //         echo "线程[{$this->name}]收到任务参数::{$this->param},需要{$nt}秒处理数据.\n";

          //         $this->res   = rand(100, 999);

          //         sleep($nt);

          //         $this->lurl = $this->param;

          //         $this->param   = '';

          //     } else {fdsdfsdfdsf

          //         echo "线程[{$this->name}]等待任务..\n";

          //     }

          //     sleep(1);

          // }

          echo "start ".$this->content_id."\n";

          $contentOrm = new \orm\mfk\content();

          $contentOrm->table_name = "content";

          $content = $contentOrm->get_one("id=".$this->content_id);

          $data = array();

          $data[] = $content['id'];

          $data[] = $content['title'];

          fputcsv($this->writer,$data);

          $this->isFinished = true;

           echo "end ".$this->content_id."\n";


    }


}

$pool = array();

// //这里创建线程池.

// $pool[] = new vote('a');

// $pool[] = new vote('b');

// $pool[] = new vote('c');


// //启动所有线程,使其处于工作状态

// foreach ($pool as $w) {

//     $w->start();

// }

$maxId = 0;

$writer = fopen("a.csv","w");

while(true)

{

  $contentOrm = new \orm\mfk\content();

  $contentOrm->table_name = "content";

  $where = "status=0 and process_date=1633575532 and doctor_id=4372";

  if($maxId)

  {

    $where.=" and id>".$maxId;

  }

  echo $where."\n";

  $list = $contentOrm->select($where,"id",5,"id asc");

  if(!$list)

  {

    break;

  }


  foreach($list as $row)

  {

    $maxId = $row['id'];

    while(true)

    {

      foreach ($pool as $key => $threads) 

      {

          if($threads['isFinished'])

          {

            unset($pool[$key]);

          }

      }

      if(count($pool)>=5)

      {

        continue;

      }

      $vote = new vote($row['id'],$writer);

      $vote->start();

      $pool[] = $vote;

      break;

    }

   

  }

}

while(true)

{

  $isWorking = false;

    foreach ($pool as $key => $threads) 

      {

          if(!$threads['isFinished'])

          {

            $isWorking = true;

            continue;

          }

      }

    if(!$isWorking)

    {

      break;

    }

}



fclose($writer);

exit;


评论