在通过第三方物流/任务执行为什么这个code失败? [英] Why does this code fail when executed via TPL/Tasks?

查看:142
本文介绍了在通过第三方物流/任务执行为什么这个code失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Net.Http使用网络资源。在一个线程中运行时,它完美的作品。当我通过第三方物流运行code,它挂起并无法完成,直到超时被击中。

I am using System.Net.Http to use network resources. When running on a single thread it works perfectly. When I run the code via TPL, it hangs and never completes until the timeout is hit.

什么情况是,所有的线程结束等待的sendTask.Result行。我不知道他们都在等待,但我认为它是值得的HttpClient的。

What happens is that all the threads end up waiting on the sendTask.Result line. I am not sure what they are waiting on, but I assume it is something in HttpClient.

联网code是:

using (var request = new HttpRequestMessage(HttpMethod.Get, "http://google.com/"))
{
    using (var client = new HttpClient())
    {
        var sendTask = client.SendAsync
              (request, HttpCompletionOption.ResponseHeadersRead);
        using (var response = sendTask.Result)
        {
            var streamTask = response.Content.ReadAsStreamAsync();
            using (var stream = streamTask.Result)
            {
                // problem occurs in line above
            }
        }
    }
}

是,我现在用的是第三方物流code如下。该做的方法包含完全相同的code以上。

The TPL code that I am using is as follows. The Do method contains exactly the code above.

var taskEnumerables = Enumerable.Range(0, 100);
var tasks = taskEnumerables.Select
            (x => Task.Factory.StartNew(() => _Do(ref count))).ToArray();
Task.WaitAll(tasks);

我已经尝试了几个不同的调度,而且我可以得到它的工作的唯一办法是写一个调度器限制了运行的任务为2或3的数量然而,即使这样有时会失败。

I have tried a couple of different schedulers, and the only way that I can get it to work is to write a scheduler that limits the number of running tasks to 2 or 3. However, even this fails sometimes.

我会认为我的问题是在HttpClient的,但对我的生活中,我看不到我的code任何共享状态。有没有人有什么想法?

I would assume that my problem is in HttpClient, but for the life of me I can't see any shared state in my code. Does anyone have any ideas?

谢谢, 埃里克

推荐答案

我终于找到了问题。问题是,HttpClient的发出自己的额外的任务,让我开始一个任务实际上可能最终产卵5个或更多的任务。

I finally found the issue. The problem was that HttpClient issues its own additional tasks, so a single task that I start might actually end spawning 5 or more tasks.

调度配置有上的任务数的限制。我开始该任务,这造成运行的任务打最大限制数。 HttpClient的然后尝试启动了自己的任务,但由于限制达到,它阻塞,直到任务的数量下降,当然这从来没有发生过,因为他们在等待我的任务完成。您好僵局。

The scheduler was configured with a limit on the number of tasks. I started the task, which caused the number of running tasks to hit the max limit. The HttpClient then attempted to start its own tasks, but because the limit was reached, it blocked until the number of tasks went down, which of course never happened, as they were waiting for my tasks to finish. Hello deadlock.

这个故事的道德:

  1. 任务可能是一个全球性的资源
  2. 经常有任务之间非显而易见的相互依存关系
  3. 在调度不容易合作
  4. 请不要认为你要么控制或调度任务数

我结束了使用另一种方法来节流连接数

I ended up using another method to throttle the number of connections.

埃里克

这篇关于在通过第三方物流/任务执行为什么这个code失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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