【问题标题】:RxJS parallel queue with concurrent workers?具有并发工作者的 RxJS 并行队列?
【发布时间】:2019-06-12 05:56:38
【问题描述】:

假设我想下载 10,000 个文件。我可以轻松地为这 10,000 个文件构建一个队列(如果可以做得更好,我很乐意听取建议),

import request from 'request-promise-native';
import {from} from 'rxjs';

let reqs = [];
for ( let i = 0; i < 10000; i++ ) {
  reqs.push(
    from(request(`http://bleh.com/${i}`))
  )
};

现在我有一个 Rx.JS 可观察数组,我从代表我的队列的 Promise 创建。现在对于我想要的行为,我想发出

  • 对服务器的三个并发请求
  • 请求完成后,我想触发一个新请求。

我可以为这个问题创建一个解决方案,但鉴于我从未使用过的 Rxjs queue 之类的东西,我想知道最正确的 Rxjs 方法是什么。

【问题讨论】:

标签: parallel-processing rxjs queue concurrent-queue


【解决方案1】:

听起来您想要支持调用者指定的最大并发订阅数的 forkJoin 等效项。

可以使用mergeMap 重新实现forkJoin 并公开concurrent 参数like this

import { from, Observable } from "rxjs";
import { last, map, mergeMap, toArray } from "rxjs/operators";

export function forkJoinConcurrent<T>(
  observables: Observable<T>[],
  concurrent: number
): Observable<T[]> {
  // Convert the array of observables to a higher-order observable:
  return from(observables).pipe(
    // Merge each of the observables in the higher-order observable
    // into a single stream:
    mergeMap((observable, observableIndex) => observable.pipe(
      // Like forkJoin, we're interested only in the last value:
      last(),
      // Combine the value with the index so that the stream of merged
      // values - which could be in any order - can be sorted to match
      // the order of the source observables:
      map(value => ({ index: observableIndex, value }))
    ), concurrent),
    // Convert the stream of last values to an array:
    toArray(),
    // Sort the array of value/index pairs by index - so the value
    // indices correspond to the source observable indices and then
    // map the pair to the value:
    map(pairs => pairs.sort((l, r) => l.index - r.index).map(pair => pair.value))
  );
}

【讨论】:

  • 很棒的工作。现在查看此内容,以确保我了解您的所有操作
【解决方案2】:

我在 2021 年遇到了同样的问题,并且能够利用 @cartant's answer,所以我想我会分享:

index.ts

import request from 'request-promise-native';
import { from, defer } from "rxjs";
import { forkJoinConcurrent } from './forkJoinConcurrent';

const handleRequest = async (id: string) => await request.get(`http://bleh.com/${id}`, { json: true });

const ids: string[] = [...Array(10000).keys()].map((k: number) => k.toString());

const concurrent: number = 3;

/* use `defer` instead of `from` to generate the Observables. 
 `defer` uses a factory to generate the promise and it will execute 
 the factory only when it is subscribed to */

const observables = ids.map((id: string) => defer(() => from(handleRequest(id))))

forkJoinConcurrent<any>(observables, concurrent).subscribe(value => console.log(value));

forkJoinConcurrent.ts

import { from, Observable } from "rxjs";
import { last, map, mergeMap, toArray } from "rxjs/operators";

export function forkJoinConcurrent<T>(
  observables: Observable<T>[],
  concurrent: number
): Observable<T[]> {
  // Convert the array of observables to a higher-order observable:
  return from(observables).pipe(
    // Merge each of the observables in the higher-order observable
    // into a single stream:
    mergeMap((observable, observableIndex) => observable.pipe(
      // Like forkJoin, we're interested only in the last value:
      last(),
      // Combine the value with the index so that the stream of merged
      // values - which could be in any order - can be sorted to match
      // the order of the source observables:
      map(value => ({ index: observableIndex, value }))
    ), concurrent),
    // Convert the stream of last values to an array:
    toArray(),
    // Sort the array of value/index pairs by index - so the value
    // indices correspond to the source observable indices and then
    // map the pair to the value:
    map(pairs => pairs.sort((l, r) => l.index - r.index).map(pair => pair.value))
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-24
    • 2022-09-30
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多