码迷,mamicode.com
首页 > 编程语言 > 详细

你可能写了个假异步,并不能提高请求线程池的吞吐量

时间:2020-08-01 21:21:12      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:work   table   bsp   调用   另一个   set   pen   http   设置   

不知道用什么词形容,就叫它假异步吧。

写异步方法,async 和 await 要一路写到底,否则就是假异步,并不能提高请求线程池的吞吐量。

真正的异步,我的理解是这样的:比如调用一个查询接口,在当前线程,把SQL扔给数据库,当前线程释放,去干别的事情,数据库查询完了,通知我,我再在另一个线程里(也可能是刚才释放的那个线程,也可能不是)拿查询结果,返回给客户端,数据库查询比较耗时,数据库查询的时候,对线程是0占用。
 
HttpUtil.HttpGet方法:
技术图片
/// <summary>
/// HttpGet
/// </summary>
/// <param name="url">url路径名称</param>
/// <param name="cookie">cookie</param>
public static string HttpGet(string url, CookieContainer cookie = null, WebHeaderCollection headers = null)
{
    try
    {
        // 设置参数
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.CookieContainer = cookie;
        request.Method = "GET";
        request.ContentType = "text/plain;charset=utf-8";

        if (headers != null)
        {
            foreach (string key in headers.Keys)
            {
                request.Headers.Add(key, headers[key]);
            }
        }

        //发送请求并获取相应回应数据
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        //直到request.GetResponse()程序才开始向目标网页发送Post请求
        Stream instream = response.GetResponseStream();
        StreamReader sr = new StreamReader(instream, Encoding.UTF8);
        //返回结果网页(html)代码
        string content = sr.ReadToEnd();
        return content;
    }
    catch (Exception ex)
    {
        LogUtil.Error(ex);
        return string.Empty;
    }
}
View Code

HttpUtil.HttpGetAsync方法:

技术图片
/// <summary>
/// HttpGetAsync
/// </summary>
/// <param name="url">url路径名称</param>
/// <param name="cookie">cookie</param>
public static async Task<string> HttpGetAsync(string url, CookieContainer cookie = null, WebHeaderCollection headers = null)
{
    try
    {
        // 设置参数
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.CookieContainer = cookie;
        request.Method = "GET";
        request.ContentType = "text/plain;charset=utf-8";

        if (headers != null)
        {
            foreach (string key in headers.Keys)
            {
                request.Headers.Add(key, headers[key]);
            }
        }

        //发送请求并获取相应回应数据
        HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
        //直到request.GetResponse()程序才开始向目标网页发送Post请求
        Stream instream = response.GetResponseStream();
        StreamReader sr = new StreamReader(instream, Encoding.UTF8);
        //返回结果网页(html)代码
        string content = sr.ReadToEnd();
        return content;
    }
    catch (Exception ex)
    {
        LogUtil.Error(ex);
        return string.Empty;
    }
}
View Code

测试代码:

技术图片
/// <summary>
/// 测试1
/// </summary>
private async void button1_Click(object sender, EventArgs e)
{
    //task是自己写的独立线程池,为了防止测试过程对CLR线程池和异步线程池造成干扰
    _task.Run(() =>
    {
        Thread.Sleep(200);

        int workerThreads1, completionPortThreads1, workerThreads2, completionPortThreads2;
        ThreadPool.GetMaxThreads(out workerThreads1, out completionPortThreads1);
        ThreadPool.GetAvailableThreads(out workerThreads2, out completionPortThreads2);
        Log("假异步 已使用辅助线程:" + (workerThreads1 - workerThreads2) + ", 已使用异步线程:" + (completionPortThreads1 - completionPortThreads2));
    });

    string str = await GetDataAsync();
}

/// <summary>
/// 测试2 
/// </summary>
private async void button2_Click(object sender, EventArgs e)
{
    //task是自己写的独立线程池,为了防止测试过程对CLR线程池和异步线程池造成干扰
    _task.Run(() =>
    {
        Thread.Sleep(200);

        int workerThreads1, completionPortThreads1, workerThreads2, completionPortThreads2;
        ThreadPool.GetMaxThreads(out workerThreads1, out completionPortThreads1);
        ThreadPool.GetAvailableThreads(out workerThreads2, out completionPortThreads2);
        Log("真异步 已使用辅助线程:" + (workerThreads1 - workerThreads2) + ", 已使用异步线程:" + (completionPortThreads1 - completionPortThreads2));
    });

    string str = await GetDataAsync2();
}

/// <summary>
/// 假异步
/// </summary>
private async Task<string> GetDataAsync()
{
    return await Task.Run<string>(() =>
    {
        //接口耗时大约1秒
        return HttpUtil.HttpGet("http://localhost:8500/api/test/TestGet?val=1", null, null);
    });
}

/// <summary>
/// 真异步
/// </summary>
/// <returns></returns>
private async Task<string> GetDataAsync2()
{
    //接口耗时大约1秒
    return await HttpUtil.HttpGetAsync("http://localhost:8500/api/test/TestGet?val=1", null, null);
}
View Code

测试截图:

技术图片

我想知道 WebRequest 类的 GetResponseAsync 方法是怎么实现的

你可能写了个假异步,并不能提高请求线程池的吞吐量

标签:work   table   bsp   调用   另一个   set   pen   http   设置   

原文地址:https://www.cnblogs.com/s0611163/p/13415212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!