从 Parallel.For 获取结果

发布于 2024-10-16 09:22:14 字数 1163 浏览 4 评论 0原文

我正在考虑使用 Parallel.For 来调用需要一段时间才能返回的 Web 服务,但是,我们知道我们可以同时调用它多次,并且不会比一次调用花费更长的时间。单次通话。

为此,我正在尝试 Parallel.For,我真的很想检查我的想法,看看它是如何工作的。我可能有点过于谨慎,因为我不想搞砸应用程序,并且我想确保如果我们走这条路,整个应用程序团队都知道访问并行代码时需要做什么。

无论如何,这是我目前的工作和理解。

public IEnumerable<HotelAvail> GetAvailability (IList<string> codes, DateTime startDate, int numNights)
{
    HotelAvail[] result = new HotelAvail[codes.Count];

    Parallel.For(0, codes.Count, i =>
        {
            string code = codes[i];
            result[i] = new AvailService().
                GetAvailability(
                    code, startDate, numNights);
        });

    return result;
}

AvailService 获取指定日期范围 (startDate + numNights) 内的房间可用性。 code 是属性的标识符。

我在开始时设置了一个正确大小的结果数组,其中有很多空槽。

然后我并行调用该服务。该服务创建一个新的 HotelAvail 对象,我将其放置在数组中的正确位置。

一切完成后,我返回数组。此时应该已完全填充。不应有空格。该服务不会影响系统状态的任何其他部分 - 它只是构建一个 Web 服务调用、调用它并返回一个结果对象。

有没有我没有看到的问题。

就像我上面说的,我可能过于谨慎,但在更年轻和精力充沛的日子里,我因编写多线程代码而感到痛苦,我不想再犯同样的错误。

此外,此代码最终将出现在 ASP.NET 应用程序中。我依稀记得它对多线程代码有很多抱怨。我在那里可能会遇到任何其他问题吗?

I am looking at using Parallel.For to call into a web service that takes a while to return, however, we know we can call it many times simultaneously and it doesn't take that much longer than a single call.

To that end I'm trying out the Parallel.For and I'm really wanting to sense check my ideas on how this would work. I'm probably being a little overly cautious as I don't want to screw up the application, and I want to ensure that if we go this route the entire application team is aware of what needs to be done when accessing parallel code.

Anyway, here is my current working and understanding.

public IEnumerable<HotelAvail> GetAvailability (IList<string> codes, DateTime startDate, int numNights)
{
    HotelAvail[] result = new HotelAvail[codes.Count];

    Parallel.For(0, codes.Count, i =>
        {
            string code = codes[i];
            result[i] = new AvailService().
                GetAvailability(
                    code, startDate, numNights);
        });

    return result;
}

The AvailService gets availability of rooms for a specified date range (startDate + numNights). The code is the identifier for the property.

I set up a result array of the correct size at the start with lots of empty slots.

Then I call the service in parallel. The service creates a new HotelAvail object and I place it in the array in the correct position.

When all is done I return the array. It should by this point be fully populated. There should be no blanks. The service does not affect any other part of system state - It just builds a web service call, calls it, and returns a result object.

Are there any issues with this that I'm not seeing.

Like I said above, I'm probably being overly cautious, but I was burned with writing multi-threaded code in more youthful and exuberant days and I don't want to make the same mistakes again.

Also, this code will ultimately end up in an ASP.NET application. I vaguely recall that it complains a lot about multi-threaded code. Any additional problems I might encounter there?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东走西顾 2024-10-23 09:22:14

对我来说看起来不错,但我认为 PLINQ 会更优雅一点:

    public IEnumerable<HotelAvail> GetAvailability (IList<string> codes, 
                                                    DateTime startDate, 
                                                    int numNights)
    {
        return codes.AsParallel().AsOrdered().Select(code => 
                 new AvailService().GetAvailability(code, startDate, numNights))
                .ToList();
    }

Looks OK to me, though I think PLINQ would be a little more elegant:

    public IEnumerable<HotelAvail> GetAvailability (IList<string> codes, 
                                                    DateTime startDate, 
                                                    int numNights)
    {
        return codes.AsParallel().AsOrdered().Select(code => 
                 new AvailService().GetAvailability(code, startDate, numNights))
                .ToList();
    }
锦爱 2024-10-23 09:22:14

对于 Asp.net 问题,如果您的方法调用没有快速返回,您很可能会遇到应用程序超时。对于此场景,您可能想要为每个代码调用一个使用 AJAX 的方法,在 Web 服务调用完成时返回一个 HotelAvail 对象,并在可用时使用新信息更新您的 UI。

For the Asp.net question, you may very well encounter the application timing out if your method call does not return quickly. What you may want to do for this scenario is to call a method using AJAX for each code, returning a HotelAvail object when your web service call completes, updating your UI with the new information when available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文