Мое использование curl_multi () все еще серийно? Если так, как я могу распараллелить? - PullRequest
1 голос
/ 23 ноября 2010

Сегодня я получил от кого-то подсказку, что мой код curl_multi() фактически работает в последовательном режиме, когда я надеялся распараллелить запросы cURL.

Является ли мой код последовательным? Если так, как я могу распараллелить?

Вот соответствующий код:

  /**
   * Returns the cURL responses given multiple target URLs
   * @param array $targetUrls Array of target URLs for cURL
   *
   * @return array cURL Responses
   */
  private function getCurlMultiResponses($targetUrls)
  {
    // Cache the count
    $count = count($targetUrls);

    // Create the multiple cURL handles
    for($i = 0; $i < $count; $i++) {
      $ch[$i] = curl_init($targetUrls[$i]);
      curl_setopt($ch[$i], CURLOPT_POST, FALSE);
      curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, TRUE);
    }

    // Initialize the multiple cURL handle
    $mh = curl_multi_init();

    // Add the handles to the curl_multi handle
    for($i = 0; $i < $count; $i++) {
      curl_multi_add_handle($mh, $ch[$i]);
    }

    $running = null;
    // Execute the handles
    do {
      curl_multi_exec($mh, $running);
    } while ($running > 0);

    $responses = array();

    // Remove the handles and return the response
    for($i = 0; $i < $count; $i++) {
      curl_multi_remove_handle($mh, $ch[$i]);

      $responses[$i] = curl_multi_getcontent($ch[$i]);
    }

    // Close the multiple cURL handle
    curl_multi_close($mh);

    return $responses;
  }

1 Ответ

1 голос
/ 23 ноября 2010

Руководство определенно предполагает, что это параллельная операция:

Позволяет параллельно обрабатывать несколько дескрипторов cURL.

Здесь есть хороший учебник .

...