2017-09-27 11 views
1

concurrency 옵션에 대한 많은 정보를 Pool에 발견하지 못했습니다.Guzzle에서 정확히 "동시성"이란 무엇입니까?

이것이 서버에서 열 수있는 TCP 소켓의 수인 경우 질문은 "요청을 더 빨리 처리하기 위해 사용할 수있는 동시성의 수는 무엇입니까?"입니다.

// I am using Laravel, this is basically retrieving entities from DB 
    $exchangers = Exchanger::all(); 

    $client = new Guzzlelient(); 

    $requests = []; 
    foreach ($exchangers as $exchanger) 
    { 
     $requests[$exchanger->id] = new Request('GET', $exchanger->xml_feed_url); 
    } 

    $pool = new Pool($client, $requests, [ 
     'concurrency' => 5, 
     'options' => [ 
      'verify' => false 
     ], 
     'fulfilled' => function ($response, $index) { 
      echo "fulfilled: " . $index."\n"; 
     }, 
     'rejected' => function ($reason, $index) { 
      echo "rejected: " . $index. "\n"; 
     }, 
    ]); 

    // Initiate the transfers and create a promise 
    $promise = $pool->promise(); 

    // Force the pool of requests to complete. 
    $promise->wait(); 

그것은 ~ 나처럼 생각하고 이제 5 로 설정 동시성 20 개 사이트 "좋아,이 번호 가서 약 10 초에 나섭니다 :

나는 Pool를 사용하는 예를 소켓 ~ 포트 : 65535 개의 포트가 있습니다. 멋지군, 동시성을 50으로 설정하지 않으면 모든 결과가 잠깐 나옵니까? " 좋아, 내가 50로 설정하고 ... 8 초가 걸렸다. 그러나 한 시간 전에 결과는 18 초 vs 24 (50 동시성, 그래서 더 느린) 같았습니다.

그래서 질문은 :

  1. 어떻게 빨리이 가능하려면로를 였는지를을 최적화하기 위해 사용할 수있는 동시성 결정?
  2. 어쨌든 동시성이란 무엇입니까?

답변

0

기대했던 것입니다. X 동시 요청을 보내지 만 동시에 concurrency 개의 요청 만 전송됩니다. 요청이 다음 다른 하나는 대기 완료 할 때마다 (source code)

다음

이이 소스에서 수행되는 방법은 다음과 같습니다

이 방법은 요청 완료 (기능 step이 후 모든 호출로 설정 될 때마다 호출

private function refillPending() 
{ 
    if (!$this->concurrency) { 
     // Add all pending promises. 
     while ($this->addPending() && $this->advanceIterator()); 
     return; 
    } 
    // Add only up to N pending promises. 
    $concurrency = is_callable($this->concurrency) 
     ? call_user_func($this->concurrency, count($this->pending)) 
     : $this->concurrency; 
    $concurrency = max($concurrency - count($this->pending), 0); 
    // Concurrency may be set to 0 to disallow new promises. 
    if (!$concurrency) { 
     return; 
    } 
    // Add the first pending promise. 
    $this->addPending(); 
    // Note this is special handling for concurrency=1 so that we do 
    // not advance the iterator after adding the first promise. This 
    // helps work around issues with generators that might not have the 
    // next value to yield until promise callbacks are called. 
    while (--$concurrency 
     && $this->advanceIterator() 
     && $this->addPending()); 
}  
failiure 콜백 성공).

그러나 일반적으로 OS 소켓이나 ISP 속도 제한 또는 원격 서버 속도 제한 (다른 요청이 동일한 서버로 이동하는 경우)과 같은 다른 제한을 두드리기 때문에 더 나은 것은 아닙니다. 대부분의 경우 최적의 값은 시행 착오를 통해 발견됩니다.

+0

내 모든 요청은 다른 서버로 이동합니다. 그래서 OS가 50 소켓처럼 여유를 가질 수 있다고 생각했는데 왜 시간 차이가 큰지 알 수 없었습니다. 25 개의 URL, 50 개의 소켓, 모든 요청에 ​​대해 1-2 초처럼? :) – Victor

+0

로컬 대역폭은 아마도 제한 요소이기도합니다. 충분한 응답을 대기 행렬에 올리면 패킷이 끊어지며 다시 요청해야 처리량이 낮아집니다. 네트워크 트래픽을 검사하여 사용률을 확인할 수도 있습니다. – apokryfos