In general, an API interface with task and async is generally an asynchronous API interface.
If I want to use the asynchronous API interface, the general motivation is that I might use task.run in my approach to handle a time-consuming operation asynchronously.
Q1. If an API interface is time-consuming, such as requiring 10s, will it affect my front-end display?
--No. Many JS can request the API interface asynchronously, and when the interface returns data, the data is displayed through the callback method.
Q2. For a time-consuming API interface, the use of asynchronous API interfaces (which may use task.run asynchronous operations inside the interface) can certainly improve the response?
--not necessarily. As an example:
Public Async task<ienumerable<string>> Get (string s) { Log.infoformat ( Itcloudlogtype.common, "Begin request.thread id:{0},time:{1},requestid:{2}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToLongTimeString (), this. ControllerContext.Request.GetCorrelationId (). ToString ()); Await Task.run (() =Job1 (s)); Log.infoformat (Itcloudlogtype.common, "End request.id:{0},time:{1},requestid:{2}", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToLongTimeString (), this. ControllerContext.Request.GetCorrelationId (). ToString ()); return new string[] {"value1", "value2" }; } private void Job1 (string s) {return;} private int Job2 () {return 2;}
Scenario 1. It is assumed that JOB1 is very time consuming and the results of JOB1 have no effect on the output of the results below , if
It seems very unwise. You can remove the await keyword, put the Job1 in an asynchronous field to execute, and the program continues to go down, to avoid unnecessary waiting, to accelerate the interface.
Situation 2. More common:The results of JOB1 are required below .
In this case, the use of await Task.run (() =>job1 (s)) is of little significance. Synchronous and asynchronous have the same efficiency.
Problems with the Web API interface for synchronization and Asynchrony