gpt4 book ai didi

asp.net-mvc - MVC3 AsyncController - 执行同步 I/O 任务

转载 作者:行者123 更新时间:2023-12-02 23:52:12 26 4
gpt4 key购买 nike

我们计划使用 AsyncController,因为我们的大多数请求将是长时间运行的 I/O 绑定(bind)请求。计划将此处理卸载到 CLR 线程,以便保持最大数量的 IIS 线程空闲来服务新的传入请求。

到目前为止,我们看到的使用 AsyncController 的每个示例都从 AsyncController 的异步操作方法内部异步执行长时间运行的 I/O 绑定(bind)进程。如果您有 2 个或更多可以在 Controller 的异步操作方法中并行运行的操作,我们可以看到这样做的值(value)。

示例: Using an Asynchronous Controller in ASP.NET MVC

在那里,他们异步执行新闻服务。这真的是必需的吗?如果新闻服务是您需要在该操作方法中执行的唯一内容,那么异步执行新闻服务似乎是多余的。在我们的例子中,我们有一个很长的 I/O 进程要运行。我们不能从操作方法的异步版本中同步运行它吗?一旦我们进入 AsyncController 中的异步操作方法,工作是否就已经被传递给 CLR 线程了?

我们再次可以看到,如果我们有更多的工作可以并行运行,我们将如何异步执行进一步的操作。然而,我们只有一个长阻塞操作,并且希望代码保持简单。

我们还看到了另一种策略,使用 Task.Factory.StartNew(),如下所示: Using the AsyncController in ASP.NET MVC 2

我们也非常不愿意这样做,因为它看起来不必要地多余。

最佳答案

是的,我相信你是对的。 documentation说明了初始化异步操作的以下步骤。

  1. The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation.

  2. The worker thread is returned to the thread pool to service another Web request.

工作线程启动异步操作(即,您不需要在代码中执行此操作)。您引用的示例中使用“额外”异步代码可能是为了说明 AsyncManager.OutstandingOperations.Increment 和 Decrement 的使用(它们通知工作进程即使异步操作可能已返回,操作仍在运行。

关于asp.net-mvc - MVC3 AsyncController - 执行同步 I/O 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12539912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com