S
S
sansansan32021-11-14 09:14:24
.NET
sansansan3, 2021-11-14 09:14:24

Does it make sense to use asynchronous calls in Don net core console programs if the processing algorithm is sequential?

Wrote a program to import data into the database from an external REST service.
The program is written as a console application on dot net core in C#.
It is scheduled to run every 30 minutes from Windows Sheduler.

The main loop looks like:

Данные = СчитатьДанныеИзREST();
while (Данные != NULL) {
      СохранитьДанныеВБд(Данные);
      Данные = СчитатьДанныеИзREST();  
}

Now I have written a program without using asynchronous methods for getting data and writing it to the database. To get data from REST and save it to the database, I use synchronous methods:
  • WebRequest GetResponse() - to get data from REST
  • Dapper Execute() - to save data to the database


As can be seen from the pseudocode of the main program loop, the data is requested and processed in a purely sequential manner. And this suits me completely - I do not need to process them in parallel / asynchronously. The program does not work with Threads in any way, and multiple copies of the program will not run at the same time.

However, to get data from REST and store it in the database, you can use asynchronous versions of the same methods (MethodNameAsync):
  • WebRequest GetResponseAsync() - to get data
  • Dapper ExecuteAsync() - to save data to the database


Question: is there any reason to redesign the program to use asynchronous methods instead of synchronous ones? Is there any benefit from this in the conditions that I described? Pluses like: the dot net core runtime will use processors time / resources more economically for other tasks, and now it just hangs stupidly waiting when it is waiting for data to be received from REST or when saving data to the database?

I understand how to write async/await when calling and in function declarations. It's not a problem for me. I understand that instead of the usual method, a state machine will be generated by the system that will eat resources.

The question can be reformulated more generally: does it make sense to use asynchronous calls in Don net core console programs if the processing algorithm is sequential?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
none7, 2021-11-14
@sansansan3

Asynchrony in C# is cooperative multitasking. If multitasking within a process is not needed, then asynchrony is also not needed. On the contrary, it will work a little slower and eat a little more memory. When a synchronous task is waiting for data, it does not consume CPU time. The thread goes to the OS kernel and the thread manager switches to another thread or puts the processor core in standby mode.

S
sansansan3, 2021-11-14
@sansansan3

Thanks for the prompt detailed answer.

When a synchronous task is waiting for data, it does not consume CPU time. The thread goes to the OS kernel and the thread manager switches to another thread or puts the processor core in standby mode.

Here I slightly did not understand - if when calling a synchronous method, the expectation is carried out "lossless", then why then all this asynchrony, because everything is fine anyway? Why then came up with async / await?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question