demo.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. function addCurl($curl, $url)
  3. {
  4. // echo 'Start: ' . $url . PHP_EOL;
  5. $options = [
  6. CURLOPT_URL => $url,
  7. CURLOPT_TIMEOUT => 30,
  8. CURLOPT_RETURNTRANSFER => true,
  9. CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
  10. ];
  11. // ssl设置
  12. if (stripos($url, 'https') === 0) {
  13. $options[CURLOPT_SSL_VERIFYPEER] = false;
  14. }
  15. $c = curl_init();
  16. curl_setopt_array($c, $options);
  17. curl_multi_add_handle($curl, $c);
  18. }
  19. $works = [];
  20. $work_num = 10;
  21. for ($i = 0; $i < $work_num; $i++) {
  22. $work = new Swoole\Process(function ($p) use ($i) {
  23. $curl = curl_multi_init();
  24. while (true) {
  25. while (($exec = curl_multi_exec($curl, $else)) == CURLM_CALL_MULTI_PERFORM);
  26. if ($exec != CURLM_OK) {
  27. break;
  28. }
  29. // 数据处理
  30. while ($data = curl_multi_info_read($curl)) {
  31. $body = curl_multi_getcontent($data['handle']);
  32. preg_match('#<title>(.*?)</title>#i', $body, $match);
  33. echo 'End: ' . $match[1] . PHP_EOL;
  34. curl_multi_remove_handle($curl, $data['handle']);
  35. }
  36. if ($else > 0) {
  37. curl_multi_select($curl);
  38. } else {
  39. echo '#' . $i . ' 空闲' . PHP_EOL;
  40. $url = $p->pop();
  41. echo '#' . $i . ' 接收到任务:' . $url . PHP_EOL;
  42. addCurl($curl, $url);
  43. }
  44. usleep(1000);
  45. }
  46. curl_multi_close($curl);
  47. });
  48. $work->useQueue();
  49. $pid = $work->start();
  50. $works[$pid] = $work;
  51. }
  52. $master = new Swoole\Process(function () {});
  53. $master->useQueue();
  54. while (true) {
  55. $master->push('https://github.com/');
  56. usleep(10);
  57. }