简体   繁体   中英

Task.Run() vs Async/Await

I have a question regarding some code I am writing. I have 3 calls made synchronously to some endpoints that have large payloads. I don't want to wait for these payloads and instead continue running through the method until I need the values from those 3 endpoints.

I have approached a solution like this. I converted the method that calls the 3 service endpoints into an async method. I start the call for the data using

var serviceCallOneTask = Task.Run(()=> serviceCallOne());

Note serviceCallOne() is not asynchronous and finally when I need the data I do something like

var serviceCallOneValue = await serviceCallOneTask;

My questions are

  1. Is this solution considered bad practice?
  2. Should I be worried about deadlocks?
  3. From what I have read, when using await we are not blocking a thread but when using task.run we are using a CPU-bound thread and we are blocking the thread pool; is that correct?
  4. Is it better for me to convert everything in this httpGet method from beginning to end into async methods?
  5. Is it ok for me to approach the problem this way for now and later on convert those task.run() services into asynchronous methods?
  1. Is this solution considered bad practice?

It depends what you're doing.

Task.Run moves execution to another thread. That is helpful in a desktop application because you don't want long-running CPU-bound operations running on the UI thread and locking up your UI.

ASP.NET is different since there is no UI thread, so there is no need to move operations to another thread, unless you want to do something in parallel (run two CPU-bound operations at the same time).

If you're doing something else between calling Task.Run and await serviceCallOneTask , then that's certainly a reason to do what you're doing. But whether it's "better" depends on what serviceCallOne() is doing. You have to think about two things to determine if the benefit outweighs the cost:

  1. Does the benefit of running it in a separate thread outweigh the cost of moving it to a separate thread? (Is it actually faster than running it in the same thread?)
  2. Remember that ASP.NET has a limited number of threads (by default, 20 per processor), and now you're using 2 threads instead of 1. Depending on the expected load of your application, that may or may not matter.
  1. Should I be worried about deadlocks?

Not with the small bit of code that you've shown. As long as you don't wait synchronously on an async method , you will not have to worry about deadlocks.

  1. From what I have read, when using await we are not blocking a thread but when using task.run we are using a CPU-bound thread and we are blocking the thread pool; is that correct?

When using await , you don't block the current thread. As discussed above, you might just be blocking another thread, depending on your code.

  1. Is it better for me to convert everything in this httpGet method from beginning to end into async methods?

Considering the limited thread that ASP.NET has, and that async / await helps you free up threads, then yes. It's always better to use async wherever you can.

  1. Is it ok for me to approach the problem this way for now and later on convert those task.run() services into asynchronous methods?

If it works, then it's "ok". But you have to change something , right? May as well do it right. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM