使用 .Net 并行扩展 (Parallel.Invoke) 进行多个异步调用?

发布于 2024-09-19 01:30:54 字数 275 浏览 3 评论 0 原文

目前,我有一段代码需要向不同的数据提供者进行大约 7 次 Web 服务调用。每个调用都需要几秒钟的时间来执行,因此我想并行运行它们以加快速度。

我已将 7 个调用包装在 Parallel.Invoke 中,它非常适合同时运行几件事,但在 2 核服务器上,它一次只能执行 2 个,每个核心一个。由于我所做的只是等待 Web 服务调用返回,所以我希望它获取所有 7 个并等待它们返回。

难道就没有办法了吗?或者也许我的方法是错误的?也许我需要创建对 Web 服务的异步调用?但如何等他们全部回来再继续前进呢?

Currently I have a section of code that needs to make about 7 web service calls to various providers for data. Each call takes a several seconds to execute, so I would like to run them in parallel to speed things up.

I have wrapped my 7 calls in a Parallel.Invoke which works great at running a couple things at the same time, but on a 2 core server, it will only execute 2 at a time, one per core. Since all I am doing is waiting around for the web service calls to return I would want it to go fetch all 7 and wait for them to come back.

Is there no way to do this? Or perhaps my approach is wrong? Maybe I need to create asynchronous calls to the web services? But then how to wait for them all to come back before moving on?

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

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

发布评论

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

评论(4

吹泡泡o 2024-09-26 01:30:54

但在 2 核服务器上,它一次只会执行 2 个,每个核一个

我对此表示怀疑 - Parallel.Invoke 通常会比系统中的核心运行更多的任务。

话虽这么说,如果您使用异步方法调用,我建议使用 Task.Factory.FromAsync(...) 生成七个不同的任务。

这提供了在任务执行时执行其他工作的灵活性,然后在决定使用结果时调用 Task.WaitAll (或 Task.WaitAny)。

另一方面,Parallel.Invoke 将始终阻塞,直到所有七个完成为止。

but on a 2 core server, it will only execute 2 at a time, one per core

I would question this - Parallel.Invoke will typically run many more tasks than cores in your system.

That being said, if you're using asynchronous method call invocations, I would recommend using Task.Factory.FromAsync(...) to generate your seven distinct tasks.

This provides the flexibility of doing other work while the tasks execute, then calling Task.WaitAll (or Task.WaitAny) when you decide to use the results.

Parallel.Invoke, on the other hand, will always block until all seven complete.

栩栩如生 2024-09-26 01:30:54

没有真正需要为此使用Parallel.Invoke。您可以继续发出多个异步请求(即HttpWebRequest.BeginGetResponse())。至于当它们全部完成时收到通知,您有多种选择。一种简单的方法是在之前初始化 CountdownEvent您发出第一个请求,然后让主线程在发出请求后等待该事件。每个异步回调方法在完成时都会发出该事件的信号。这样,您可以确保在主线程继续之前所有请求都已完成。

There's no real need to use Parallel.Invoke for this. You can go ahead and issue the multiple asynchronous requests (i.e. HttpWebRequest.BeginGetResponse()). As for being notified when they all complete, you have several options. A simple way would be to initialize a CountdownEvent before you issue the first request, and then have the main thread wait on that event after it's issued the requests. The asynchronous callback methods each signal that event as they complete. That way, you ensure that all requests have completed before your main thread continues.

魂牵梦绕锁你心扉 2024-09-26 01:30:54

您可以指定 ParallelOptions 来增加并发级别。默认值对于 CPU 密集型任务来说是合理的,但您正在处理 I/O 密集型任务,因此覆盖该行为是有意义的。

You can specify ParallelOptions to increase the concurrency level. The default is reasonable for CPU-bound tasks, but you're dealing with I/O-bound ones, so it makes sense to override that behavior.

兮子 2024-09-26 01:30:54

我将复制我对另一个问题逐字逐句,因为它同样(如果不是更多的话)适用于此。


在 I/O 性能方面,您根本无法击败异步编程模型 (APM)。任何时候你都应该使用它。幸运的是,任务并行库 (TPL) 支持通过 FromAsync 工厂方法

查看 MSDN 上 .NET SDK 的这一部分,标题为 TPL 和传统 .NET 异步编程,了解有关如何结合这两种编程模型以实现异步必杀技的更多信息。

I'm going to copy a response that I made to another question verbatim because it's equally, if not more, applicable here.


You simply cannot beat the Asynchronous Programming Model (APM) when it comes to I/O performance. Anytime you can use it, you should be. Luckily the Task Parallel Library (TPL) comes with baked in support for combining APM work into the mix with "pure" TPL tasks via the FromAsync factory method.

Check out this section of the .NET SDK on MSDN entitled TPL and Traditional .NET Asynchronous Programming for more information on how to combine these two programming models to achieve async nirvana.

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