博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
考虑用Task.WhenAll
阅读量:4033 次
发布时间:2019-05-24

本文共 4202 字,大约阅读时间需要 14 分钟。

异步能在一定场景中带性能的飞跃,同步调用性能,也以带来时间的节省。

先看一下被调用的api:

using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace WebAPI.Controllers{    [ApiController]    [Route("[controller]")]    public class HomeController : ControllerBase    {        private readonly ILogger
_logger; public HomeController(ILogger
logger) { _logger = logger; } [HttpGet("/api001")] public async Task
GetAPI001() { _logger.LogInformation("GetAPI001"); await Task.Delay(1000); return new JsonResult(new { result = true, data = "api001 返回成功" }); } [HttpGet("/api002")] public async Task
GetAPI002() { _logger.LogInformation("GetAPI002"); await Task.Delay(1000); if (DateTime.Now.Second % 2 == 0) { throw new Exception("api002异常"); } return new JsonResult(new { result = true, data = "api002 返回成功" }); } [HttpGet("/api003")] public async Task
GetAPI003() { _logger.LogInformation("GetAPI003"); await Task.Delay(1000); return new JsonResult(new { result = true, data = "api003 返回成功" }); } }}

调用时反序列化的实体类

class ResponseResult
{ public bool Result { get; set; } public string Message { get; set; } public T Data { get; set; } }

三个api的调用方法

private static async Task
GetAPI001(HttpClient httpClient){ var content = await httpClient.GetStringAsync("http://localhost:5000/api001"); var result = JsonSerializer.Deserialize
>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (result.Result) { return result.Data; } else { return result.Message; }}private static async Task
GetAPI002(HttpClient httpClient){ var content = await httpClient.GetStringAsync("http://localhost:5000/api002"); var result = JsonSerializer.Deserialize
>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (result.Result) { return result.Data; } else { return result.Message; }}private static async Task
GetAPI003(HttpClient httpClient){ var content = await httpClient.GetStringAsync("http://localhost:5000/api003"); var result = JsonSerializer.Deserialize
>(content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (result.Result) { return result.Data; } else { return result.Message; }}

同步的调用方式

static async Task SyncCall(){    using var httpClient = new HttpClient();    try    {        var result1 = await GetAPI001(httpClient);        WriteLine(result1);    }    catch (Exception exc)    {        WriteLine(exc.Message);    }    try    {        var result2 = await GetAPI002(httpClient);        Console.WriteLine(result2);    }    catch (Exception exc)    {        WriteLine(exc.Message);    }    try    {        var result3 = await GetAPI003(httpClient);        Console.WriteLine(result3);    }    catch (Exception exc)    {        WriteLine(exc.Message);    }}

调用方式

static async Task Main(string[] args){    while (true)    {        WriteLine("回车开始执行");        ReadLine();        var stopwatch = Stopwatch.StartNew();        await SyncCall();        WriteLine($"用时{stopwatch.ElapsedMilliseconds}ms");    }}

同步的调用,运行三次调用是三次的时间,3202ms,如果有异常不干扰其他api调用。

static async Task AsyncCall(){    using var httpClient = new HttpClient();    var allTasks = Task.WhenAll(GetAPI001(httpClient), GetAPI002(httpClient), GetAPI003(httpClient));    try    {        var results = await allTasks;        foreach (var result in results)        {            Console.WriteLine(result);        }    }    catch (Exception exc)    {        Console.WriteLine($"捕捉到的异常:{exc.Message}");    }    if (allTasks.Exception != null)    {        Console.WriteLine($"AllTasks异常:{ allTasks.Exception.Message}");    }}

同步调用成功时间是1156ms,时间缩短了,但三个api调用,如果有异常,则全军覆没。

先择适合的方式,打造更优的应用。

转载地址:http://irydi.baihongyu.com/

你可能感兴趣的文章
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
从头开始学习JSP(3)——一些配置
查看>>
html常用标签快速检索
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
通过/proc/PID/status查看进程内存占用情况
查看>>
/proc文件系统读出来的数据是最新的吗?
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
python单元测试unittest学习
查看>>
Errors running builder 'Validation' on project 'jumi_3.0'
查看>>