简体   繁体   中英

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. 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:

  • ups_Proc1 returns a List of usp_Proc1
  • usp_Proc2 returns a List of usp_Proc2

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. So how does one fire multiple stored procedures in parallel when dealing with different complex return types?

UPDATE- 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() .

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. 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.

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() .

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. 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.

Any feedback is greatly appreciated!

Task.WhenAll is supported for Task inputs which you have. So use Task.WhenAll first to make sure all tasks are completed. Then, use Task.Result to get the actual results from all tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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