ConcurrentRequest - marmot-cn/marmot-framework GitHub Wiki

并行请求

概述

使用GuzzleAdapter并行请求多条数据.

并行请求优点:

  • 请求时间等于MAX(并行请求) 但是 小于超时时间, 即总的返回时间等于最长的请求的时间, 但是小于最长的时间.
  • 如果某个请求有错误或者无结果, 则返回空数组.

示例

1. 初始化concurrentAdapter

$this->concurrentAdapter = new ConcurrentAdapter();

2. 并行请求

GuzzleAdapter中声明方法getAsync: 异步get请求.

$this->getConcurrentAdapter()->addPromise(
    'notice',
    $repository->searchAsync($noticeFilter, $sort, DEFAULT_PAGE, INDEX_LIST_SIZE),
    $repository->getAdapter()
);
$this->getConcurrentAdapter()->addPromise(
    映射key,
    异步请求,
    适配器
);

$data = $this->getConcurrentAdapter()->run();

$data是并行返回结果数组.

  • 如果有返回数据, 则返回array(映射key=>结果数组)
  • 如果没有返回数据, 则返回