Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear outstanding request before invoking callback #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

clear outstanding request before invoking callback #21

wants to merge 1 commit into from

Conversation

redcapital
Copy link

When you put new requests into queue by calling "startRequest" inside a callback, the outstanding request never gets a chance to be cleaned up from queue and the program gets stuck. Demonstrated by this simple script:

<?
require 'parallelcurl.php';
$pc = new ParallelCurl(5);

$callback = function($content, $url, $ch, $userParams) use (&$callback, $pc) {
  // Print status of finished request
  $i = $userParams['i'];
  $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);    
  echo "Request index {$i}, got http code: $httpcode\n";

  if ($i == 20) {
    // That's enough
    echo "Finished!\n";
    return;
  }

  // Continue with next request
  // This is a common usecase when next request url depends on contents of current page
  $pc->startRequest('example.com', $callback, ['i' => $i + 1]);
};

$pc->startRequest('example.com', $callback, ['i' => 1]);
$pc->finishAllRequests(); 

Try running it without patch (script gets stuck after 5 requests) and with patch (script correctly performs 20 requests).
The proposed change fixes it by first removing outstanding request from the queue and then invoking the callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants