php 结合pcntl_fork导出excel数据 使用

// 任务列表

  $userInfo = $this->user->getUserInfo(0, 50000);
  // 存储子进程的PID
  $childPids = [];

  for ($i = 1; $i <= 20; $i++) {
   $pid = pcntl_fork();

   if ($pid == -1) {
    die("无法创建子进程");
   } elseif ($pid == 0) {
    $is = ($i - 1) * 1000;
    $test = array_slice($userInfo, $is, 1000);

    foreach ($test as $key => $vals) {
     $childPids[] = array(
      'name' => decryptInfo($vals['name']),
      'hash' => $this->pub->decrypt($vals['hash'], $keypws),
      'dzyj' => decryptInfo($vals['dzyj']),
      'sj' => decryptInfo($vals['sj'])
     );
    }
    file_put_contents(APPPATH . 'data/test.json', json_encode($childPids), FILE_APPEND);
    exit(); // 子进程执行完毕后退出
   }
  }

  // 父进程等待所有子进程完成
  while (pcntl_waitpid(0, $status) != -1) {
   $status = pcntl_wexitstatus($status);
   echo "子进程 {$status} 已完成\n";
  }
``
posted @ 2023-10-26 16:55  尘梦  阅读(20)  评论(0编辑  收藏  举报