public function test_paralell_download() { $this->assertEmptyPool(); for ($i = 0; $i < 2; ++$i) { Curl::download('http://bfanger.nl/', \Sledgehammer\TMP_DIR . 'curltest' . $i . '.downoad', [], true); } Curl::synchronize(); $this->assertEmptyPool(); for ($i = 0; $i < 2; ++$i) { unlink(\Sledgehammer\TMP_DIR . 'curltest' . $i . '.downoad'); } }
/** * Start the request * 1. Set the CURLOPT_* options * 2. Add the request to the pool * 3. Starts the request. * * @param array $options CURLOPT_* options * * @throws Exception */ private function start($options) { $this->state = 'ERROR'; self::$requests[] = $this; // Watch changes // Setting options foreach ($options as $option => $value) { if (curl_setopt($this->handle, $option, $value) === false) { throw new Exception('Setting option:' . self::optionName($option) . ' failed'); } $option = self::optionName($option); $this->options[$option] = $value; } // multi curl init if (self::$pool === null) { self::$pool = curl_multi_init(); if (self::$pool === false) { throw new Exception('Failed to create cURL multi handle'); } if (self::$keepalive) { register_shutdown_function(function () { Curl::$keepalive = false; // Close the multi handle when all requests are completed. Curl::synchronize(); }); } } // Wait until a new tranfer can be added. self::throttle(self::$maxConcurrent - 1); // Add request $error = curl_multi_add_handle(self::$pool, $this->handle); while ($error === CURLM_CALL_MULTI_PERFORM) { $error = curl_multi_exec(self::$pool, $active); } if ($error !== CURLM_OK) { throw new Exception('[' . self::multiErrorName($error) . '] Failed to add cURL handle'); } ++self::$tranferCount; // Start request do { $error = curl_multi_exec(self::$pool, $active); } while ($error === CURLM_CALL_MULTI_PERFORM); if ($error !== CURLM_OK) { throw new Exception('[' . self::multiErrorName($error) . '] Failed to execute cURL multi handle'); } $this->state = 'RUNNING'; }