使用Web服务来避免线程并行(异步)任务 [英] Parallel(Async) tasks using Web Service to avoid Threads

查看:192
本文介绍了使用Web服务来避免线程并行(异步)任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的并行执行任务;例如打印用户选择文件async'ly。一种方式可以是使用一个工作线程。但是,在成千上万的请求的浇筑到Web服务器和应用程序产卵多一个线程用于打印的情景思维听起来太可怕了。而如果所有的并发用户数开始打印?

I have a task that I perform in parallel; for instance printing user selected documents async'ly. One way may be to use a worker thread. But thinking of the scenario where thousands of requests pouring in onto the web server, and application is spawning one more thread for printing sounds horrible. And what if all of the concurrent users start printing?

那么,我想避免的工作线程的原因。

So is the reason I want to avoid worker threads.

要解决,我提出了Web服务的code;我打电话了 PrintAsync()的方法,我已经订阅了 OnPrintComplete 来得到通知。现在,我可以给,因为我想尽可能多的打印,而不必担心asp.net线程饥饿或阻止请求。

To work around, I have moved the code in a web service; I am calling the PrintAsync() method, and I have subscribed to OnPrintComplete to get notified. Now I can send as many prints as I want without worrying about asp.net thread starvation or blocking requests.

我知道Web服务的内部的使用线程,但毕竟是IOCP线程,这意味着它不会打扰asp.net工作线程。

I know web services internally use threading, but that is IOCP threads, which means it will not bother the asp.net worker threads.

我想不出可能的利弊,除了这将是一个Web服务。

I couldn't think of possible cons, except that it will be a web service.

这是一个好方法?什么将是一个<击>更好替代的处理这种功能的版本?

Is this a good approach? What would have been a better alternate version of handling this functionality?

推荐答案

所以,你所描述你如何做客户端上的异步调用,实际上有一些更多的问题,我想请教一下,你是如何真正被完全异步在那里,但它似乎是你的问题更多的是如何尽可能高效的服务端吧?

So you've described how you're making async calls on the client and there's actually some more questions I would ask about how you're actually being completely async there, but it seems like your question is more about how to be as efficient as possible on the service side, right?

如果您正在执行长在你的服务操作或运行I / O密集​​型操作,你绝对的必须的开始利用了的异步服务操作。现在,有很多方法可以做到这一点,但如果你在.NET 4.0没有比使用的任务并行库(TPL)。

If you're performing long running or I/O bound operations in your service operations, you absolutely must begin to leverage WCF's support for asynchronous service operations. Now, there's lots of ways to do this, but if you're on .NET 4.0 there's no better way than using the Task Parallel Library (TPL).

首先,通过卸载工作到TPL线程你腾出WCF I / O线程来处理更多的呼叫。这样,你的长期运行的WCF操作不占用WCF的还田等作业的能力。

First, by offloading the work to a TPL thread you free up WCF I/O threads to handle more calls. This way your long running WCF operations don't tie up WCF's ability to field other operations.

二,TPL利用默认的线程池。你不必担心每一个操作旋转起来它自己的线程,并最终饥饿资源的机器。第三方物流也足够聪明为s $ P $垫在包装盒上的方式在所有核心的工作更有效地比你所能做到自己没有显著的投资以书面水暖code。

Second, the TPL leverages the a thread pool by default. You don't have to worry about every operation spinning up it's own thread and eventually starving the machine of resources. The TPL is also smart enough to spread the work across all the cores on the box way more efficiently than you could ever do yourself without a significant investment in writing plumbing code.

三,第三方物流可以与传统的异步编程模型相结合(APM) 所以,如果你的东西的工作,比如(网络或文件),您可以用它们的的BeginRead /写的方法来利用异步I / O,以将腾出CPU线程同时阻止对读/写最高。你绝对应该这样做,即使你不使用的第三方物流,以实现最大效率,TPL只是更容易。

Third, the TPL can be combined with the traditional Asynchronous Programming Model (APM) so that if you're working with things like Streams (network or file) you can use their BeginRead/Write methods to leverage async I/O to the max which will free up the CPU threads while blocking on reads/writes. You should absolutely be doing this to achieve maximum efficiency even if you're not using the TPL, the TPL just makes it easier.

下面是如何使用第三方物流来实现异步服务操作的裸露的骨头的例子:

Here's a "bare bones" example of how you can use the TPL to implement an async service operation:

public IAsyncResult BeginSomeLongRunningOperation(string sampleParam, AsyncCallback callback, object asyncState)
{
    Task<int> processingTask = Task<int>.Factory.StartNew(
        _ =>
        {
             ... perform insanely long running operation here ...

             return 42;    
        },
        asyncState);

    // If there was a callback, we have to invoke it after the processing finishes
    if(callback != null)
    {
        processingTask.ContinueWith(
            _ =>
            {
                callback(calculationTask);                   
            },
            TaskContinuationOptions.ExecuteSynchronously);
    }

    return processingTask;
}

public int EndSomeLongRunningOperation(IAsyncResult asyncResult)
{
    return ((Task<int>)asyncResult).Result;
}

这篇关于使用Web服务来避免线程并行(异步)任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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