异步运行的方法相同的多个实例? [英] Run multiple instances of same method asynchronously?

查看:209
本文介绍了异步运行的方法相同的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是相当怪异。

My requirement is quite weird.

的someMethod()这就要求 GetDataFor()

public void SomeMethod()
{
    for(int i = 0; i<100; i++) {
        var data = GetDataFor(i);
    }
}

public data GetDataFor(int i) {
    //call a remote API
    //to generate data for i
    //store to database
    return data;
}

对于每个 I ,最终结果将总是是不同的。没有必要的的为 GetDataFor(我)调用之前完成<$​​ C $ C> GetDataFor第(i + 1)。

For each i, the end result will always be different. There is no need to wait for GetDataFor(i) to complete before calling GetDataFor(i+1).

在换句话说,我需要:


  • 通话 GetDataFor()每个 I + 1 立即成功后调用 I (称他们并行看起来不可能的)

  • 等到全部 GetDataFor的100个实例()完成后运行

  • 将范围的someMethod()

  • call GetDataFor() for each i+1 immediately after successfully calling i (Calling them in parallel looks impossible)
  • wait until all the 100 instances of GetDataFor() are completed running
  • leave the scope of SomeMethod()

继href=\"http://stackoverflow.com/a/16728712/2404470\"> YK1的回答,我试图修改它像这样

Following YK1's answer, I have tried to modify it like this:

public async Task<void> SomeMethod()
{
    for(int i = 0; i < 100; i++) {
        var task = Task.Run(() => GetDataFor(i));
        var data = await task;
    }
}

它没有任何抛出的错误,但我需要了解这背后的理念:

It didn't thrown any errors but I need to understand the concept behind this:


  • 如何任务将为等待 ING不同的呼叫进行区分?它越来越覆盖。

  • 这是公然错误的方式做到这一点?那么,怎样做是正确的?

  • How task will distinguish between different calls for awaiting? It is getting over-written.
  • Is it blatantly wrong way to do this? So, how do do it right?

推荐答案

您可以使用的Parallel.For

public void SomeMethod()
{
    Parallel.For(0, 100, i =>
    {
        var data = GetDataFor(i);
        //Do something
    });
}

public data GetDataFor(int i)
{
    //generate data for i
    return data;
}

编辑:

并行循环的语法是非常相似的的foreach 循环您已经知道了,但并行循环运行具有可用内核的计算机上更快。另一个不同之处在于,不像顺序循环,执行的顺序不用于并行循环限定。步骤常常发生在同一时间,在平行。有时,两个步骤发生在相反的顺序,他们会比如果环路有顺序的。唯一的保证是所有循环的迭代将由循环结束的时候已经运行。

The syntax of a parallel loop is very similar to the for and foreach loops you already know, but the parallel loop runs faster on a computer that has available cores. Another difference is that, unlike a sequential loop, the order of execution isn't defined for a parallel loop. Steps often take place at the same time, in parallel. Sometimes, two steps take place in the opposite order than they would if the loop were sequential. The only guarantee is that all of the loop's iterations will have run by the time the loop finishes.

有关并行循环,并行度并不需要由您code指定。相反,运行时环境的同时尽可能多的内核执行,因为它可以循环的步骤。环路正常工作,无论多核心是如何使用。如果只有一个核心,性能接近(也许内的几个百分点)的顺序相当的。如果有多个芯,性能得到改善;在许多情况下,性能与内核的数量成比例地提高。

For parallel loops, the degree of parallelism doesn't need to be specified by your code. Instead, the run-time environment executes the steps of the loop at the same time on as many cores as it can. The loop works correctly no matter how many cores are available. If there is only one core, the performance is close to (perhaps within a few percentage points of) the sequential equivalent. If there are multiple cores, performance improves; in many cases, performance improves proportionately with the number of cores.

您可以看到更详细的解释这里

You can see a more detailed explanation here.

这篇关于异步运行的方法相同的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆