简体   繁体   English

任务并行库占用生产服务器上的大量空间

[英]Task Parallel Library consuming lots of space on production server

I am using – 我在用 -

Task task = new Task(delegate { GetRecordsForEmailReplies(headingList, partialEntity); });
task.Start();

to run some heavy methods, but the problem is it's consuming lots of space of CPU on server some time IIS Work process increased above 60% thats why server gets stuck. 运行一些繁琐的方法,但是问题是,IIS工作进程在一段时间内消耗了服务器上大量CPU的空间,这就是为什么服务器卡住了60%以上。

Is there any solution to manage this problem, so please let me know? 有没有解决此问题的解决方案,所以请告诉我? or any other option to run these heavy method without blocking the page load? 或任何其他选项来运行这些繁重的方法而不会阻塞页面加载?

The suggested means for creating a new Task or Task<T> is to use Task.Factory.StartNew with .NET 4.0 or Task.Run with .NET 4.5 . 创建新TaskTask<T>的建议方法是将Task.Factory.StartNew.NET 4.0一起使用,或者将Task.Run.NET 4.5一起使用 There is a detailed explanation from Stephen Toub here on the topic. Stephen Toub对此有详细的解释。

He explains that it's more efficient: 他解释说,这样更有效:

For example, we take a lot of care within TPL to make sure that when accessing tasks from multiple threads concurrently, the "right" thing happens. 例如,我们在TPL中非常小心,以确保从多个线程同时访问任务时,“正确”的事情发生了。 A Task is only ever executed once, and that means we need to ensure that multiple calls to a task's Start method from multiple threads concurrently will only result in the task being scheduled once. 一个Task只会执行一次,这意味着我们需要确保从多个线程并发地多次调用该任务的Start方法只会导致该任务被调度一次。

Again, use the Task.Run instead: 再次使用Task.Run代替:

Task.Run(() => GetRecordsForEmailReplies(headingList, partialEntity));

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

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