简体   繁体   English

并行运行多个任务

[英]Running Multiple Tasks in Parallel

I have a list of proxies, each proxy goes to various sites and pulls the needed data from the sites. 我有一个代理列表,每个代理都转到各个站点,并从站点中提取所需的数据。 Currently it's doing this one at a time. 目前,它一次执行一次。 But I'd like to have 10 - 20 tasks running at once so it's downloading from 20 sites in one go rather than just one. 但是我想一次运行10到20个任务,因此可以一口气从20个站点下载,而不仅仅是一个。

Here's how I'm currently doing it: 这是我目前的操作方式:

private async Task<string> DownloadDataFromSite(string url)
{
     // (await) Do Work.
    return HTMLSourceCode;
}

I then loop through the proxies 然后我遍历代理

foreach(Proxy p in proxies)
{
    string source = await DownloadDataFromSite(site);
}

Is Parallel.ForEach suitable for such a task? Parallel.ForEach是否适合此类任务? I've tried it, but the problem I'm having at the moment is not being able to await . 我已经尝试过了,但是目前遇到的问题是无法await

One way is to avoid awaiting in the foreach. 一种方法是避免在foreach中等待。 The thing is that your await effectively blocks your execution. 问题是您的await有效地阻止了您的执行。 A better way might be something like this: 更好的方法可能是这样的:

await Task.WhenAll(proxies.Select(p => DownloadDataFromSite(site)));

This will mean you'll be awaiting all the tasks at once, which means the asynchronous I/O is going to happen in parallel. 这意味着您将立即等待所有任务,这意味着异步I / O将并行发生。 Note that if you're doing CPU work too, that's not going to really be parallelized. 请注意,如果您也正在执行CPU工作,那将不会真正实现并行化。

The point is, asynchronous I/O (such as downloading a web page) doesn't require more threads to run in parallel. 关键是,异步I / O(例如下载网页)不需要并行运行更多线程。 On the other hand, Parallel.ForEach is actually intended for CPU-bound work, rather than I/O bound work, and it does execute the code on multiple threads. 另一方面, Parallel.ForEach实际上是用于CPU绑定的工作,而不是I / O绑定的工作,它确实在多个线程上执行代码。

PArallel.ForEach does not work well as it expects a synchronous lambda and giving it an asynchronous one basically causes it to return as soon as it starts. PArallel.ForEach不能正常运行,因为它需要同步的lambda,而给它一个异步的lambda基本上会导致它在启动后立即返回。 There is a way around it though, check this question out: Is it OK to do some async/await inside some .NET Parallel.ForEach() code? 但是,有一种解决方法,请检查以下问题: 是否可以在某些.NET Parallel.ForEach()代码内进行异步/等待?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM