4

我有一个流程,我需要对服务器进行约 100 次 http api 调用并处理结果。我已经把这个命令执行器放在一起,它构建了一个命令列表,然后异步运行它们。进行大约 100 次调用并解析结果需要超过 1 分钟。使用浏览器的 1 个请求会在大约 100 毫秒内给我一个响应。你会认为大约 100 次调用大约需要 10 秒。我相信我做错了什么,这应该会更快。

 public static class CommandExecutor
 {
    private static readonly ThreadLocal<List<Command>> CommandsToExecute =
        new ThreadLocal<List<Command>>(() => new List<Command>());
    private static readonly ThreadLocal<List<Task<List<Candidate>>>> Tasks =
        new ThreadLocal<List<Task<List<Candidate>>>>(() => new List<Task<List<Candidate>>>());

    public static void ExecuteLater(Command command)
    {
        CommandsToExecute.Value.Add(command);
    }

    public static void StartExecuting()
    {
        foreach (var command in CommandsToExecute.Value)
        {
            Tasks.Value.Add(Task.Factory.StartNew<List<Candidate>>(command.GetResult));
        }

        Task.WaitAll(Tasks.Value.ToArray());
    }

    public static List<Candidate> Result()
    {
        return Tasks.Value.Where(x => x.Result != null)
                          .SelectMany(x => x.Result)
                          .ToList();
    }
}

我传递到此列表的命令创建了一个新的 httpclient,使用 url 在该客户端上调用 getasync,将字符串响应转换为对象,然后对字段进行水合。

    protected void Initialize()
    {
        _httpClient = new HttpClient();
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
    }

    protected override void Execute()
    {
        Initialize();

        var task = _httpClient.GetAsync(string.Format(Url, Input));
        Result = ConvertResponseToObjectAsync(task).Result;
        Result.ForEach(x => x.prop = value);
    }

    private static Task<Model> ConvertResponseToObjectAsync(Task<HttpResponseMessage> task)
    {
        return task.Result.Content.ReadAsAsync<Model>(
           new MediaTypeFormatter[]
           {
                 new Formatter()
           });
    }

您能否了解我的瓶颈或对如何加快速度有任何建议。

编辑 进行这些更改使其缩短到 4 秒。

protected override void Execute()
    {
        Initialize();

        _httpClient.GetAsync(string.Format(Url, Input))
        .ContinueWith(httpResponse => ConvertResponseToObjectAsync(httpResponse)
        .ContinueWith(ProcessResult));
    }

    protected void ProcessResult(Task<Model> model)
    {
        Result = model.Result;
        Result.ForEach(x => x.prop = value);
    }
4

2 回答 2

5

停止创建新的 HttpClient 实例。每次您处理 HttpClient 实例时,它都会关闭 TCP/IP 连接。创建一个 HttpClient 实例并为每个请求重新使用它。HttpClient 可以同时在多个不同的线程上发出多个请求。

于 2014-03-08T02:45:36.503 回答
2

避免task.Result在 ConvertResponseToObjectAsync 中使用,然后在Execute. 而是将这些链接到原始GetAsync任务ContinueWith

就目前而言,Result将阻止当前线程的执行,直到其他任务完成。但是,您的线程池将很快得到等待其他无处运行的任务的任务的支持。最终(等待一秒钟后),线程池将添加一个额外的线程来运行,因此这最终会完成,但这几乎没有效率。

作为一般原则,您应该避免Task.Result在任务继续时访问。

作为奖励,您可能不想使用ThreadLocalStorage. ThreadLocalStorage将存储在其中的项目的实例存储在访问它的每个线程上。在这种情况下,您似乎想要一种线程安全但共享的存储形式。我会推荐ConcurrentQueue这种事情。

于 2012-06-28T02:27:09.023 回答