服务器

位置:IT落伍者 >> 服务器 >> 浏览文章

Asp.net异步如何提高服务器吞吐量


发布日期:2019年03月20日
 
Asp.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的异步能够提高服务器的吞吐量但是我始终无法理解他是怎么提高其处理能力的

上一篇:7.4.4 DetaiIsView和FormView控件[2]

下一篇:制作ASP脚本组件实现重启服务器