简体   繁体   English

ASP.Net页面或Async页面中可接受的基于任务的编程是首选方法

[英]Is Task-based programming acceptable in an ASP.Net page or Async page is the preferred approach

I am using the following Task based programming (TPL) in a page that is not an Async page. 我在不是异步页面的页面中使用以下基于任务的编程(TPL)。

My question is: Is it ok to use TPL (ie Task Parallel Library) for creating multi-threaded calls in an ASP.Net page, or one must always use the inbuilt Async page feature for multi-threading in an ASP.Net page? 我的问题是: 使用TPL(即任务并行库)在ASP.Net页面中创建多线程调用是否可以,或者必须始终在ASP.Net页面中使用内置的异步页面功能进行多线程处理? So far the TPL approach hasn't created any problems, but just wanted to be sure in case I missed some important point/hidden risks with using TPL in an ASP.Net page. 到目前为止,TPL方法没有产生任何问题,但只是想确保我在ASP.Net页面中使用TPL时遗漏了一些重要的点/隐藏风险。

       Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Method1(); 
                        success4 = true;
                    }
                    catch (Exception e4)
                    {
                        success4 = false;
                        ex = e4.ToString();
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Method2();
                        success5 = true;
                    }
                    catch (Exception e5)
                    {
                        success5 = false;
                        ex = e5.ToString();
                    }
                }),
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Method3();
                        success6 = true;
                    }
                    catch (Exception e6)
                    {
                        success6 = false;
                        ex = e6.ToString();
                    }
                })
            };
   Task.WaitAll(tasks);

UPDATE 1: 更新1:

As pointed out in the answer, using TPL in ASP.Net would limit scalability of the ASP.Net app, since each task uses a thread from ASP.Net thread pool, and at the same time keeps the original thread of the page in a blocked state. 正如在答案中指出的那样,在ASP.Net中使用TPL会限制ASP.Net应用程序的可伸缩性,因为每个任务都使用来自ASP.Net线程池的线程,同时保持页面的原始线程在封锁状态。
I am now thinking about using TPL when calling the method in an Async page as in code below, to take advantage of more powerful processing that parallelism brings. 我正在考虑在Async页面中调用方法时使用TPL,如下面的代码所示,以利用并行性带来的更强大的处理。 But I will investigate this further to see whether in a real situation, TPL will make it better or worse for the async method. 但我会进一步研究这一点,看看在实际情况下,TPL是否会使异步方法更好或更差。

private void DoTask4(object[] paras)
    {
        Parallel.Invoke(() =>
            {
               try
               {
                   Method4( paras);
               }
               catch (Exception e4)
               {
                   success4 = false;
                   //log the exception
               }
           });
    }

    IAsyncResult BeginAsyncOperation4(object sender, EventArgs e, AsyncCallback cb, object state)
    {
        task4 = new AsyncTaskDelegate(DoTask4);
        IAsyncResult result = task4.BeginInvoke(state as object[], cb, "task4");
        return result;
    }

    void EndAsyncOperation4(IAsyncResult ar)
    {
        task4.EndInvoke(ar);
        if (success4 == null)
        {
            success4 = true;
        }
    }

    void TimeoutAsyncOperation4(IAsyncResult ar)
    {
        success4 = false;
    }

UPDATE 2: 更新2:

Finally, as pointed out in the answer, even using TPL with Async page method would also cause a thread from thread pool to get used, which again means our ASP.Net scalability will be an issue. 最后,正如答案中所指出的,即使使用TPL和Async页面方法也会导致线程池中的线程被使用,这再次意味着我们的ASP.Net可伸缩性将成为一个问题。 So, its best not to use TPL inside an Async page. 因此,最好不要在Async页面中使用TPL。

This approach may seriously hurt the server scalability, because it uses extra theads and blocks the request thread. 这种方法可能严重损害服务器的可伸缩性,因为它使用额外的theads并阻塞请求线程。 Suppose Method1..3 are responsible for some IO-bound work. 假设Method1..3负责一些IO绑定工作。 If you can't use async ASP.NET controllers, you still can start an async IO-bound operation with AsyncManager (eg, like this ) or with PageAsyncTask (for classic ASP.NET) and avoid blocking. 如果您不能使用异步ASP.NET控制器,您仍然可以使用AsyncManager (例如,像这样 )或使用PageAsyncTask (对于经典ASP.NET)启动异步IO绑定操作并避免阻塞。

If however you need to do some CPU-bound work (as opposed to IO-bound work), just do it on the same request thread, step by step, rather than in parallel. 但是,如果你需要做一些CPU绑定工作(而不是IO绑定工作),只需在同一个请求线程上,一步一步,而不是并行执行。 Otherwise, each Task.Factory.StartNew may be putting another incoming HTTP request on hold, depending on the server load. 否则,每个Task.Factory.StartNew可能会暂停另一个传入的HTTP请求,具体取决于服务器负载。 Because you probably want to serve as many concurrent HTTP requests as possible, you should avoid parallelizing CPU-bound work. 因为您可能希望尽可能多地提供并发HTTP请求,所以应避免并行化CPU绑定工作。

That said, your code might be fine for an intranet web app with low number of concurrent HTTP requests. 也就是说,您的代码可能适用于并发HTTP请求数较少的Intranet Web应用程序。 You could do so if you want to speed up the response delivery for an individual request. 如果您想加快单个请求的响应传递,则可以这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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