gpt4 book ai didi

c# - PLINQ 查询给出溢出异常

转载 作者:太空狗 更新时间:2023-10-29 20:24:47 24 4
gpt4 key购买 nike

我正在运行一个 PLINQ 查询,如下所示:

ParallelQuery<string> winningCombos = from n in nextComboMaker.GetNextCombo()
.AsParallel().WithCancellation(_cancelSource.Token)
where ComboWasAWinner(n)
select n;

ConcurrentBag<string> wins = new ConcurrentBag<string>();

foreach (var winningCombo in winningCombos)
{
wins.Add(winningCombo);
if (wins.Count == _maxWinsAllowed)
break;
}

GetNextCombo 方法只是返回下一个字母和数字的组合,可能性高达数十亿/万亿。

现在,当我选择的组合范围大于 Int32 的允许大小时,它会抛出异常,它总是在运行的组合计数器为 2147483584 时抛出。

我通过创建一个每次返回的假组合(执行 yield return "234gf24fa23..."等)确保它在 GetNextCombo 中没有任何内容

异常被 LINQ 抛出:

System.AggregateException was unhandled by user code
Message=One or more errors occurred.
Source=System.Core
StackTrace:
at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose)
at System.Linq.Parallel.MergeExecutor`1.Execute[TKey](PartitionedStream`2 partitions, Boolean ignoreOutput, ParallelMergeOptions options, TaskScheduler taskScheduler, Boolean isOrdered, CancellationState cancellationState, Int32 queryId)
at System.Linq.Parallel.PartitionedStreamMerger`1.Receive[TKey](PartitionedStream`2 partitionedStream)
at System.Linq.Parallel.ForAllOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings)
at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
at System.Linq.Parallel.WhereQueryOperator`1.WrapPartitionedStream[TKey](PartitionedStream`2 inputStream, IPartitionedStreamRecipient`1 recipient, Boolean preferStriping, QuerySettings settings)
at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.ChildResultsRecipient.Receive[TKey](PartitionedStream`2 inputStream)
at System.Linq.Parallel.ScanQueryOperator`1.ScanEnumerableQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
at System.Linq.Parallel.UnaryQueryOperator`2.UnaryQueryOperatorResults.GivePartitionedStream(IPartitionedStreamRecipient`1 recipient)
at System.Linq.Parallel.QueryOperator`1.GetOpenedEnumerator(Nullable`1 mergeOptions, Boolean suppressOrder, Boolean forEffect, QuerySettings querySettings)
at System.Linq.Parallel.ForAllOperator`1.RunSynchronously()
at StockWiz.Library.PLINQArrayProcessor.DoProcessing() in C:\Users\dad\Documents\BitBucket\stockwiz_clone\stockwiz\StockWiz.Library\PLINQArrayProcessor.cs:line 50
at System.Threading.Tasks.Task.Execute()
InnerException: System.OverflowException
Message=Arithmetic operation resulted in an overflow.
Source=System.Core
StackTrace:
at System.Linq.Parallel.PartitionedDataSource`1.ContiguousChunkLazyEnumerator.MoveNext(T& currentElement, Int32& currentKey)
at System.Linq.Parallel.WhereQueryOperator`1.WhereQueryOperatorEnumerator`1.MoveNext(TInputOutput& currentElement, TKey& currentKey)
at System.Linq.Parallel.ForAllOperator`1.ForAllEnumerator`1.MoveNext(TInput& currentElement, Int32& currentKey)
at System.Linq.Parallel.ForAllSpoolingTask`2.SpoolingWork()
at System.Linq.Parallel.SpoolingTaskBase.Work()
at System.Linq.Parallel.QueryTask.BaseWork(Object unused)
at System.Threading.Tasks.Task.Execute()
InnerException:

我想知道是否有什么我可以做的来改变这个查询不溢出,我做事的任何顺序等等。也许没有 linq 查询做一个 where 和选择,虽然我试过这个:

 var query = nextComboMaker.GetNextCombo().AsParallel();

query.ForAll(x => if(ComboWasAWinner(x) wins.Add(x) );

还是一样的溢出。

最佳答案

您应该能够通过使用自定义分区程序来解决这个问题。在这种情况下,PLINQ 选择的默认分区程序仅支持 int 范围内的项目数。

参见 http://msdn.microsoft.com/en-us/library/dd997416.aspx了解如何做到这一点。

关于c# - PLINQ 查询给出溢出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13131957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com