【发布时间】:2021-07-15 21:44:34
【问题描述】:
第一个函数旨在使 linq 能够安全地并行执行 lambda 函数(甚至是 async void 函数)。
所以你可以做 collection.AsParallel().ForAllASync(async x => await x.Action)。
第二个函数旨在使您能够并行组合和执行多个 IAsyncEnumerables 并尽快返回它们的结果。
我有以下代码:
public static async Task ForAllAsync<TSource>(
this ParallelQuery<TSource> source,
Func<TSource, Task> selector,
int? maxDegreeOfParallelism = null)
{
int maxAsyncThreadCount = maxDegreeOfParallelism ?? Math.Min(System.Environment.ProcessorCount, 128);
using SemaphoreSlim throttler = new SemaphoreSlim(maxAsyncThreadCount, maxAsyncThreadCount);
IEnumerable<Task> tasks = source.Select(async input =>
{
await throttler.WaitAsync().ConfigureAwait(false);
try
{
await selector(input).ConfigureAwait(false);
}
finally
{
throttler.Release();
}
});
await Task.WhenAll(tasks).ConfigureAwait(true);
}
public static async IAsyncEnumerable<T> ForAllAsync<TSource, T>(
this ParallelQuery<TSource> source,
Func<TSource, IAsyncEnumerable<T>> selector,
int? maxDegreeOfParallelism = null,
[EnumeratorCancellation]CancellationToken cancellationToken = default)
where T : new()
{
IEnumerable<(IAsyncEnumerator<T>, bool)> enumerators =
source.Select(x => (selector.Invoke(x).GetAsyncEnumerator(cancellationToken), true)).ToList();
while (enumerators.Any())
{
await enumerators.AsParallel()
.ForAllAsync(async e => e.Item2 = (await e.Item1.MoveNextAsync()), maxDegreeOfParallelism)
.ConfigureAwait(false);
foreach (var enumerator in enumerators)
{
yield return enumerator.Item1.Current;
}
enumerators = enumerators.Where(e => e.Item2);
}
}
在迭代器到达末尾后,代码会以某种方式继续返回结果。
我正在使用这些函数来组合多个 IAsyncEnumerable 函数线程,这些函数调用 API 端点,但相同类型的结果除外。
为什么?
【问题讨论】:
-
这似乎与您的帖子an hour ago 非常相似。发生了什么变化?
-
不同的问题。修复 ToList() 后,现在代码不会停止返回结果。根据我对接受的答案的理解,其余代码应该是正确的。
-
关于返回
IAsyncEnumerable<T>的第二个ForAllAsync方法,您可能需要查看System.Interactive.Async 包中AsyncEnumerableEx.Merge运算符的实现。该运算符具有以下签名:public static IAsyncEnumerable<TSource> Merge<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources); -
这看起来很像我所需要的。谢谢!不过我很好奇我做错了什么。
标签: c# multithreading linq parallel-processing iasyncenumerable