curl_multi_exec example



        // Step through the task queue which may add additional requests.         P\Utils::queue()->run();

        if ($this->active && \curl_multi_select($this->_mh, $this->selectTimeout) === -1) {
            // Perform a usleep if a select returns -1.             // See: https://bugs.php.net/bug.php?id=61141             \usleep(250);
        }

        while (\curl_multi_exec($this->_mh, $this->active) === \CURLM_CALL_MULTI_PERFORM) {
        }

        $this->processMessages();
    }

    /** * Runs until all outstanding connections have completed. */
    public function execute(): void
    {
        $queue = P\Utils::queue();

        
$multi->handlesActivity[(int) $response->handle][] = null;
                $multi->handlesActivity[(int) $response->handle][] = new TransportException(sprintf('Userland callback cannot use the client nor the response while processing "%s".', curl_getinfo($response->handle, \CURLINFO_EFFECTIVE_URL)));
            }

            return;
        }

        try {
            $multi->performing = true;
            ++$multi->execCounter;
            $active = 0;
            while (\CURLM_CALL_MULTI_PERFORM === ($err = curl_multi_exec($multi->handle, $active))) {
            }

            if (\CURLM_OK !== $err) {
                throw new TransportException(curl_multi_strerror($err));
            }

            while ($info = curl_multi_info_read($multi->handle)) {
                if (\CURLMSG_DONE !== $info['msg']) {
                    continue;
                }
                $result = $info['result'];
                
return $pushedResponse ?? new CurlResponse($this->multi, $ch$options$this->logger, $method, self::createRedirectResolver($options$host$port), CurlClientState::$curlVersion['version_number']$url);
    }

    public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
    {
        if ($responses instanceof CurlResponse) {
            $responses = [$responses];
        }

        if ($this->multi->handle instanceof \CurlMultiHandle) {
            $active = 0;
            while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi->handle, $active)) {
            }
        }

        return new ResponseStream(CurlResponse::stream($responses$timeout));
    }

    public function reset(): void
    {
        $this->multi->reset();
    }

    
$completed       = 0;
        $responses       = [];
        $subrequestcount = count($subrequests);

        $request['options']['hooks']->dispatch('curl.before_multi_exec', [&$multihandle]);

        do {
            $active = 0;

            do {
                $status = curl_multi_exec($multihandle$active);
            } while ($status === CURLM_CALL_MULTI_PERFORM);

            $to_process = [];

            // Read the information as needed             while ($done = curl_multi_info_read($multihandle)) {
                $key = array_search($done['handle']$subhandles, true);
                if (!isset($to_process[$key])) {
                    $to_process[$key] = $done;
                }
            }

            
Home | Imprint | This part of the site doesn't use cookies.