异步调用库函数的相互依托,如何处理? [英] async library calls relying on each other and how to handle?

查看:118
本文介绍了异步调用库函数的相互依托,如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从previous问题,我发布<一个一个遵循href=\"http://stackoverflow.com/questions/32606485/calling-an-async-method-using-a-task-run-seems-wrong\">Calling使用Task.Run异步方法似乎是错误的?

this is a follow on from a previous question I posted Calling an async method using a Task.Run seems wrong?

我想承包商写的code是错误的,但下面就从提供的答案,现在我想知道,如果它的库故障。这个库暴露了两个方法,我需要使用。返回一个模板,一个占用该模板(它在我的实现反正)的一部分。但两者都是异步方法返回任务

I thought the code written by the contractor was wrong but following on from the answers provided I'm now wondering if it's the libraries fault. This library exposes two methods that I need to use. One returns a "template" and one consumes part of this template (it does in my implementation anyway). But both are async methods returning Tasks.

要解释一下我的库有方法:

To explain my library has methods:

public Task<TemplateInfo> TemplateInfo(TemplateInfoRequest request);
public Task<List<EmailResult>> SendMessageTemplate(SendMessageTemplateRequest request);

我需要这样调用这些:

I need to call these thus:

public bool SendMessage()
{
  var template = TemplateInfo(...);
  var message = //use template to create message
  var sendResponse = SendMessageTemplate(message);

   return sendResponse.Count > 0;
}

所以第二个呼叫依靠第一。这是异步没有道理给我。 <击>我不能,也不想在并行运行这些。我想按顺序运行它们。我要按synchonous方法。

So the second call relies on the first. This is where the async doesn't make sense to me. I can't and don't want to run these in parallel. I want to run them in sequence. I want my method to by synchonous.

在我的previous问题的答案还指出:

An answer in my previous question also states:

既然你在异步方法阻止(,你不应该这样做
  有机会的话,你会死锁。

Since you're blocking on an async method (which you shouldn't do) there is a chance you'll deadlock.

那么,如何获得这样的方式,这些异步方法,他们依次处理,并返回一个结果synconhonous但不导致死锁?

So how do I access these async methods in such a way that they are processed in turn and return a synconhonous result but do not cause deadlocks?

推荐答案

异步不(一定)是指平行的。

Async doesn't (necessarily) means parallel.

您可以拨打这两个异步方法的依序没有它们并行运行。简单地调用第一和的await 返回的任务,然后做同样的第二位。

You can call both async method sequentially without them running in parallel. Simply call the first and await the returned task and then do the same with the second.

var template = await TemplateInfo(...);
var message = //use template to create message
var sendResponse = await SendMessageTemplate(message);

这是仍然有用相比,同步code,因为同时异步操作跑有没有需要螺纹等你的线程可以在您的应用程序的其他部分去工作。

This is still useful compared to synchronous code because while the asynchronous operation is "running" there's no thread needed and so your threads can go work on other parts of your application.

如果您将调用都才把等待返回的任务并行,你会(可能)运行:

If you would call both and only then await the returned tasks you will (maybe) run in parallel:

var templateTask = TemplateInfo(...);
var sendResponseTask = SendMessageTemplate(message);
await templateTask;
await sendResponseTask;

这篇关于异步调用库函数的相互依托,如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆