熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Asp.net異步如何提高服務器吞吐量

2022-06-13   來源: .NET編程 

Code
public class Handler :IHttpAsyncHandler
{
IHttpAsyncHandler 成員#region IHttpAsyncHandler 成員
public IAsyncResult BeginProcessRequest(HttpContext context AsyncCallback cb object extraData)
{
contextResponseWrite(<p>Begin IsThreadPoolThread is + ThreadCurrentThreadIsThreadPoolThread+ThreadCurrentThreadManagedThreadId + </p>\r\n);
AsyncHandler hand = new AsyncHandler(cbcontextextraData);
handStartAsyncWork();
return hand;
}
public void EndProcessRequest(IAsyncResult result)
{

}
#endregion
IHttpHandler 成員#region IHttpHandler 成員

public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
#endregion
}
public class AsyncHandler : IAsyncResult
{
private bool _completed;
private Object _state;
private AsyncCallback _callback;
private HttpContext _context;
public AsyncHandler(AsyncCallback callback HttpContext context Object state)
{
_callback = callback;
_context = context;
_state = state;
_completed = false;
}

IAsyncResult 成員#region IAsyncResult 成員
public object AsyncState
{
get
{
return _state;
}
}

public SystemThreadingWaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}

public bool CompletedSynchronously
{
get { return false; }
}

public bool IsCompleted
{
get { return _completed; }
}

#endregion

public void StartAsyncWork()
{
ThreadPoolQueueUserWorkItem(new WaitCallback(StartAsyncTask) null);
}
private void StartAsyncTask(Object workItemState)
{
ThreadSleep();//模擬IO
_contextResponseWrite(<p>Completion IsThreadPoolThread is + ThreadCurrentThreadIsThreadPoolThread + </p>\r\n);
_contextResponseWrite(Hello World from Async Handler! + ThreadCurrentThreadManagedThreadId);
_completed = true;
_callback(this);
}
}

  這是Aspnet中的異步處理Aspnet的異步處理目的就是要搞高服務器的接受請求的處理能力但有一點不明白使用異步處理時在BeginProcessRequest時會將處理當前請求的線程放回到線程池中放回到線程池中的此線程繼續接受新的請求但是在StartAsyncWork這個時候同樣會從當前線程池中獲取一個線程繼續處理這個IO這樣並不能提高服務器的吞吐量相反會在線程間切換浪費CPU時間和資源Aspnet異步處理就沒有意義了(其實是有意義的)

  問題Aspnet的異步能夠提高服務器的吞吐量但是我始終無法理解他是怎麼提高其處理能力的


From:http://tw.wingwit.com/Article/program/net/201311/15432.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.