[英]Running Different Stored Procedures in Parallel in EF6
I have many different stored procedures that I want to fire off in Parallel to improve the performance of my MVC web application. 我有许多不同的存储过程,我想在并行启动,以提高我的MVC Web应用程序的性能。 These stored procedures gather information from multiple sources and return different data types.
这些存储过程从多个源收集信息并返回不同的数据类型。 For the sake of simplicity, let's say I only had two, returning different complex data types:
为简单起见,假设我只有两个,返回不同的复杂数据类型:
and so on.... 等等....
I could do one of these numbers, but it would fire them off in a series: 我可以做其中一个数字,但它会在一系列中解雇它们:
List<usp_Task1> taskOneResult = await db.usp_Task1(parms).AsQueryable()
.ToListAsync();
List<usp_Task2> taskTwoResult = await db.usp_Task2(parms).AsQueryable()
.ToListAsync();
The solutions I've seen use await Task.WhenAll()
and you pass in an array of tasks, but mine are tasks with different return types. 我见过的解决方案使用
await Task.WhenAll()
并传入一组任务,但我的是具有不同返回类型的任务。 So how does one fire multiple stored procedures in parallel when dealing with different complex return types? 那么在处理不同的复杂返回类型时,如何并行激发多个存储过程呢?
UPDATE- 5/20/2015 更新 - 5/20/2015
Finally got a working solution that I believe is handling all the tasks in parallel. 最后得到了一个可行的解决方案,我相信它是并行处理所有任务的。 I had to use
Task.Factory.StartNew()
command to create each function as a task to be passed into Task.WaitAll()
. 我不得不使用
Task.Factory.StartNew()
命令将每个函数创建为要传递给Task.WaitAll()
的任务。
I rapped all this into a new class that contained a List<>
for each stored procedure return type I'm working with, allowing me to handle all my business logic there. 我把所有这些都打包成了一个新类,它包含了我正在使用的每个存储过程返回类型的
List<>
,允许我处理我的所有业务逻辑。 The code in the constructor looked a little like this: 构造函数中的代码看起来有点像这样:
var task1 = Task.Factory.StartNew(() => CallStoredProc1(parms));
var task2 = Task.Factory.StartNew(() => CallStoredProc2(parms));
var taskList = new List<Task> { task1, task2 };
Task.WaitAll(taskList.ToArray());
CallStoredProc1()
and CallStoredProc2()
are private void methods where I do my stored procedure calls and handle data conversion. CallStoredProc1()
和CallStoredProc2()
是私有的void方法,我在那里执行存储过程调用并处理数据转换。
Any feedback is greatly appreciated! 非常感谢任何反馈!
Figured I would repost my update as a solution. 想我会将我的更新重新发布为解决方案。
Finally got a working solution that I believe is handling all the tasks in parallel. 最后得到了一个可行的解决方案,我相信它是并行处理所有任务的。 I had to use
Task.Factory.StartNew()
command to create each function as a task to be passed into Task.WaitAll()
. 我不得不使用
Task.Factory.StartNew()
命令将每个函数创建为要传递给Task.WaitAll()
的任务。
I rapped all this into a new class that contained a List<>
for each stored procedure return type I'm working with, allowing me to handle all my business logic there. 我把所有这些都打包成了一个新类,它包含了我正在使用的每个存储过程返回类型的
List<>
,允许我处理我的所有业务逻辑。 The code in the constructor looked a little like this: 构造函数中的代码看起来有点像这样:
var task1 = Task.Factory.StartNew(() => CallStoredProc1(parms));
var task2 = Task.Factory.StartNew(() => CallStoredProc2(parms));
var taskList = new List<Task> { task1, task2 };
Task.WaitAll(taskList.ToArray());
CallStoredProc1()
and CallStoredProc2()
are private void methods where I do my stored procedure calls and handle data conversion. CallStoredProc1()
和CallStoredProc2()
是私有的void方法,我在那里执行存储过程调用并处理数据转换。
Any feedback is greatly appreciated! 非常感谢任何反馈!
Task.WhenAll
is supported for Task
inputs which you have. Task.WhenAll
支持您拥有的Task
输入。 So use Task.WhenAll
first to make sure all tasks are completed. 因此,首先使用
Task.WhenAll
确保完成所有任务。 Then, use Task.Result
to get the actual results from all tasks. 然后,使用
Task.Result
从所有任务中获取实际结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.