One, the thread pool settings in ASP.
in ASP. NET service processing, each time the server receives a request, httpruntime will fetch a HttpApplication object from the HttpApplication pool to handle the request, and the processing of the request will be queued to the thread pool, and for ASP. The parameters in the thread pool can be set in the processmodel section of the. config file.
ASP. NET thread-dependent parameter configuration:
| Parameters |
Configuration |
| AutoConfig |
Automatic provisioning of server-based configuration. |
| maxWorkerThreads |
Set the maximum number of worker threads per CPU, which can be set to 5~100, default to 20, and recommended to 100 |
| minWorkerThreads |
Set the minimum number of worker threads per CPU, which defaults to 1 |
| Maxiothreads |
Configure the maximum number of I/O threads per CPU, the range can be set to 5~100, the default is 20, the recommended setting is 100 |
| Miniothreads |
Configure the minimum number of worker threads per CPU, which defaults to 1 |
Configuration parameters for the httpruntime element:
| Parameters |
Configuration |
| minFreeThreads |
The minimum number of free threads that are reserved for processing new requests, with a default value of 8. It is recommended that each CPU be set to 88. These minimum idle threads are used to prevent deadlocks from being available without threads. Therefore, when the thread pool's threads are less than this number, the requests are queued and not processed using these threads. |
| minLocalRequestFreeThreads |
The minimum number of free threads reserved for local host requests, with a default value of 4. It is recommended that each CPU be set to 76. Ibid. (only for local requests) |
| appRequestQueueLimit |
When ASP. NET does not have enough threads to process the request, the requests are queued for processing, which sets the length of the queue, which returns 503 when the queue length exceeds this parameter, and defaults to 5000. |
The first principle of optimization is to use one thread to complete the processing for each request, whenever possible.
Asynchronous points in an asynchronous step
In the process of HttpApplication, in order to increase the utilization of the thread, only one thread is used to complete processing for one request.
Because the processing mode of the pipeline is used by ASP, the logical order of the processing steps must be ensured. Therefore, some processing must be done before the subsequent processing, so unless you really need multiple computationally intensive tasks that can be parallelized at this time, starting multiple threads does not improve the processing speed of the site.
For processing steps for each event in the HttpApplication processing pipeline, there are two ways to process it synchronously and asynchronously:
- Synchronization: Provides an event handling method to complete the processing step directly;
- Asynchronous: A process step for initiating an I/O, a processing step after I/O is completed;
For a processing step that needs to wait, we can separate an asynchronous point, start the time-consuming operation before the asynchronous point, and then directly end the current thread, and do this time-consuming input and output task without thread involvement ( time-consuming but no thread-involvement phase, called the async point ), After the task completes, retrieve a thread from the thread pool to continue the processing of the current request.
such as synchronous beginrequest corresponds to the asynchronous processing method defined as follows:
public void Addonbeginrequestasync (BeginEventHandler bh, endeventhandler eh)
Where BeginEventHandler is used to start the processing of the delegate, EndEventHandler is used to handle the delegate after the completion of the task.
For the processing of the HttpApplication pipeline, the delegate type of the synchronization method used by these events is the EventHandler type, which is defined as follows:
public delegate void EventHandler (Object sender, EventArgs e)
For asynchronous processing, the corresponding two delegates are used, one for the start of the delegate BeginEventHandler, and one for the end of the delegate EndEventHandler. The delegate that ends the operation will work on a thread that is provided by a thread pool.
in asynchronous mode, the processing pipeline will no longer use one thread to complete, but each processing step may be on one thread, Therefore, we cannot assume that the processing pipeline is always in one thread and uses thread-based characteristics.
Iii. starting and completing asynchronous steps
In ASP., the objects that start and complete the asynchronous step are as follows:
- The delegate type that initiates the asynchronous step is System.Web.BeginEventHandler.
- The delegate type that completes the asynchronous step is System.Web.EndEventHandler.
Its delegate is defined as follows:
Public delegate IAsyncResult BeginEventHandler (Object sender,eventargs e,asynccallback cb,object extradata) public delegate void EndEventHandler (IAsyncResult ar)
As you can see, it's actually exactly the same type as the AsyncCallback delegate.
Where the AsyncCallback type defines the type of method to call after completion of processing, this type is defined as follows:
public delegate void AsyncCallback (IAsyncResult ar)
Extradata, however, defines a custom parameter object that can be passed to the AsyncCallback method.
Iv. Asynchronous Processing Programs
For synchronous handlers, the methods defined in interface IHttpHandler are processed processrequest. This method is defined as follows:
void ProcessRequest (HttpContext context)
For asynchronous handlers, this method is also replaced by two methods, which are the two methods defined in the asynchronous handler interface IHttpAsyncHandler
- BeginProcessRequest;
- endprocessrequests;
If a handler implements an asynchronous interface, HttpApplication uses an asynchronous method to invoke the handler.
1. Asynchronous Handler Interface
The asynchronous handler interface is defined under namespace system.web:
public interface ihttpasynchandler:ihttphandler{IAsyncResult BeginProcessRequest (HttpContext context, AsyncCallback CB, Object Extradata); void EndProcessRequest (IAsyncResult result); BOOL IsReusable {get;} void ProcessRequest (HttpContext context);}
2. Invoking the Web service asynchronously in the handler
For a typical Web service, we need to start the method of invoking the Web service at the last call of the BeginProcessRequest method, and then this method can be ended.
The system then makes a lengthy web service call with no threads involved, and when the service invocation is complete, ASP. NET will queue the endprocessrequest into the I/O thread of the thread pool. Finally, the endprocessrequest will be called. In this method, we get the data returned by the service by invoking the method of ending the Web service.
V. Asynchronous pages
Page pages are the most commonly used page-generation technology in ASP. NET Web site development, essentially, it is also a handler. For the purpose of the page class, it is primarily used to generate special handlers for HTML.
This handler implements the IHttpHandler interface by default, so it can be seen as a synchronous handler. If we need to implement an asynchronous page handler, we do not need to manually modify the interface it implements, but instead use the page instruction async to specify it. When set to True, indicates that the generated page class will implement interface IHttpAsyncHandler. This allows the page object to be accessed through an asynchronous method.
<%@ page async= "true"%>
In this case, the process of page processing will be divided into two parts: from the start to including the PreRender processing part is handled by the BeginProcessRequest method in the IHttpAsyncHandler interface , from prerendercomplete to processing ends will be handled by the EndProcessRequest method. Between PreRender and Prerendercomplete is the asynchronous point of the Async page. This process is used to wait for long-time asynchronous operations to complete.
1. Start and finish of page asynchronous task
The AddOnPreRenderCompleteAsync method of the Page object is used to register the two methods of an asynchronous operation, both of which are called before and after the asynchronous point.
The two delegates are:
- BeginEventHandler;
- EndEventHandler;
2. Asynchronous page Tasks
After ASP. NET 2.0, multiple asynchronous operations can be registered with PageAsyncTask in the processing of the page, and each operation is defined by PageAsyncTask.
public sealed class PageAsyncTask
When using this class, the async state of the page can be set to true, or it can be set to false. When set to False, the processing thread of the page is blocked, so the page should be set to an asynchronous state when using the asynchronous page task.
Through the constructor of the class, we define the starting processing method of the asynchronous task, the completion of the processing method, and even provide a time-out processing method. One of the most complex constructors is as follows:
Public PageAsyncTask (
BeginEventHandler Beginhandler,
EndEventHandler Endhandler,
EndEventHandler Timeouthandler,
Object State,
BOOL Executeinparallel
)
The Registerasynctack (pageasynctask Task) of a Page object can register an asynchronous task:
public void RegisterAsyncTask (PageAsyncTask Task)
3. Three ways to access Web services in an async page
Temporarily Skip