并行调用API。有时会返回不完整的响应

发布于 2025-02-03 17:15:20 字数 2099 浏览 4 评论 0 原文

我深表歉意,无法正确地构架问题:

问题:是否有可能使用 Parallel.Foreach (使用 conturrentbag 添加 foreach 的响应)正在导致不完整的响应发送到调用方法。

在下面的代码中: processEdrecs 仅获得 1 tuple< list< sample1>,sampleclass2,ponsecteClass> list list 但是它应该得到超过1 。这很少发生,但是我无法弄清楚是什么原因引起的。

我已经研究了所有日志/DB调用,方法 SampleReq 中没有故障。

public ResponseClass2 ManageOrders(SampleRequest request)
{
    //.
    //.
    //.
    var processedRecs = FetchOrderInfo(request);

    // here "processedRecs" are getting only 1
    // Tuple<List<Sample1>, SampleClass2, ResponseClass> in the List
    foreach (var tp in processedRecs)
    {
        //.
        //.
        //.
        // but "SampleReq" method had processed 3, so the List<Touple....>
        // should have count 3 instead of 1.
    }
}

private List<Tuple<List<Sample1>, SampleClass2, ResponseClass>> FetchOrderInfo(
    SampleRequest request)
{
    //.
    //.
    //Processing the request... no issues here
    //.

    ConcurrentBag<Tuple<List<Sample1>, SampleClass2, ResponseClass>> tp =
        new ConcurrentBag<Tuple<List<Sample1>, SampleClass2, ResponseClass>>();

    Parallel.ForEach(arranged,
    samObj =>
    {
        var sampleResponse = TEngine.SampleReq(request.Email, samObj.Item2,
            request.pIndex)
                .GetAwaiter()
                .GetResult();
        tp.Add(Tuple.Create(samObj.Item1, samObj.Item2, sampleResponse));
    });

    return new List<Tuple<List<Sample1>, SampleClass2, ResponseClass>>(tp);
}

// Method "SampleReq" calls another API to get the results... we are calling this
// multiple times for different set of data and then merging this info.
public static async Task<ResponseClass> SampleReq(string email,
    SampleClass2 selectedObj, int pIndex)
{
    // These responses are getting logged and we didn't get any issues here.
}

I apologize to be not able to frame the question properly:

Question: Is there a possibility that the usage of Parallel.ForEach (used ConcurrentBag to add response from ForEach) is causing incomplete response to be sent to the calling method.

In below code: processedRecs is getting only 1 Tuple<List<Sample1>, SampleClass2, ResponseClass> in the List but it should get more than 1. This happens very rarely, but I am not able to figure out what's causing this.

I have looked into all the logs/DB calls, there were no failures in the method SampleReq.

public ResponseClass2 ManageOrders(SampleRequest request)
{
    //.
    //.
    //.
    var processedRecs = FetchOrderInfo(request);

    // here "processedRecs" are getting only 1
    // Tuple<List<Sample1>, SampleClass2, ResponseClass> in the List
    foreach (var tp in processedRecs)
    {
        //.
        //.
        //.
        // but "SampleReq" method had processed 3, so the List<Touple....>
        // should have count 3 instead of 1.
    }
}

private List<Tuple<List<Sample1>, SampleClass2, ResponseClass>> FetchOrderInfo(
    SampleRequest request)
{
    //.
    //.
    //Processing the request... no issues here
    //.

    ConcurrentBag<Tuple<List<Sample1>, SampleClass2, ResponseClass>> tp =
        new ConcurrentBag<Tuple<List<Sample1>, SampleClass2, ResponseClass>>();

    Parallel.ForEach(arranged,
    samObj =>
    {
        var sampleResponse = TEngine.SampleReq(request.Email, samObj.Item2,
            request.pIndex)
                .GetAwaiter()
                .GetResult();
        tp.Add(Tuple.Create(samObj.Item1, samObj.Item2, sampleResponse));
    });

    return new List<Tuple<List<Sample1>, SampleClass2, ResponseClass>>(tp);
}

// Method "SampleReq" calls another API to get the results... we are calling this
// multiple times for different set of data and then merging this info.
public static async Task<ResponseClass> SampleReq(string email,
    SampleClass2 selectedObj, int pIndex)
{
    // These responses are getting logged and we didn't get any issues here.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文