你可能写了个假异步,并不能提高请求线程池的吞吐量
标签:work table bsp 调用 另一个 set pen http 设置
不知道用什么词形容,就叫它假异步吧。
写异步方法,async 和 await 要一路写到底,否则就是假异步,并不能提高请求线程池的吞吐量。
真正的异步,我的理解是这样的:比如调用一个查询接口,在当前线程,把SQL扔给数据库,当前线程释放,去干别的事情,数据库查询完了,通知我,我再在另一个线程里(也可能是刚才释放的那个线程,也可能不是)拿查询结果,返回给客户端,数据库查询比较耗时,数据库查询的时候,对线程是0占用。
HttpUtil.HttpGet方法:
///
/// HttpGet
///
/// url路径名称
/// cookie
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方法:
///
/// HttpGetAsync
///
/// url路径名称
/// cookie
public static async Taskstring> 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
测试代码:
///
/// 测试1
///
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();
}
///
/// 测试2
///
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();
}
///
/// 假异步
///
private async Taskstring> GetDataAsync()
{
return await Task.Runstring>(() =>
{
//接口耗时大约1秒
return HttpUtil.HttpGet("http://localhost:8500/api/test/TestGet?val=1", null, null);
});
}
///
/// 真异步
///
///
private async Taskstring> 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
评论