c#并行IO完成端口
我正在尝试找出等待一些I/O完成端口完成的最佳方法 对于这个场景,假设我在一个MVC3 web应用程序中。(我的理解是,建议在此处使用I/O完成端口,以便将原始线程返回到IIS以服务其他请求) 假设我有一个ID数组,我想从某个网络调用中为每个ID获取一个对象 并行这种同步方法的最佳方式是什么c#并行IO完成端口,c#,.net,multithreading,asp.net-mvc-3,io-completion-ports,C#,.net,Multithreading,Asp.net Mvc 3,Io Completion Ports,我正在尝试找出等待一些I/O完成端口完成的最佳方法 对于这个场景,假设我在一个MVC3 web应用程序中。(我的理解是,建议在此处使用I/O完成端口,以便将原始线程返回到IIS以服务其他请求) 假设我有一个ID数组,我想从某个网络调用中为每个ID获取一个对象 并行这种同步方法的最佳方式是什么 public class MyController: Controller { public ActionResult Index(IEnumerable<int> i
public class MyController: Controller
{
public ActionResult Index(IEnumerable<int> ids)
{
ids.Select(id => _context.CreateQuery<Order>("Orders")
.First(o => o.id == id));
DataServiceQuery<Order> query = _context.CreateQuery<Order>("Orders");
return Json(query);
}
private DataServiceContext _context; //let's ignore how this would be populated
}
公共类MyController:Controller
{
公共操作结果索引(IEnumerable ID)
{
选择(id=>_context.CreateQuery(“订单”)
。首先(o=>o.id==id));
DataServiceQuery查询=_context.CreateQuery(“订单”);
返回Json(查询);
}
private DataServiceContext _context;//让我们忽略如何填充它
}
我知道它会这样开始:
public class MyController: AsyncController
{
public void IndexAsync(IEnumerable<int> ids)
{
// magic here...
AsyncManager.Sync(() => AsyncManager.Parameters["orders"] = orders);
}
public ActionResult IndexCompleted(IEnumerable<Order> orders)
{
return Json(orders);
}
private DataServiceContext _context; //let's ignore how this would be populated
}
公共类MyController:AsyncController
{
公共无效索引同步(IEnumerable ID)
{
//这里有魔力。。。
AsyncManager.Sync(()=>AsyncManager.Parameters[“orders”]=orders);
}
公共行动结果已完成(IEnumerable订单)
{
返回Json(订单);
}
private DataServiceContext _context;//让我们忽略如何填充它
}
我应该使用这个方法吗?我使用的数据服务一次只能获取一条记录(这超出了我的控制范围),我希望这些单独的查询能够并行运行。使用TPL很容易
public ActionResult Index(IEnumerable<int> ids)
{
var result = ids.AsParallel()
.Select(id => GetOrder(id))
.ToList();
return Json(result);
}
Order GetOrder(int id) { ... }
公共操作结果索引(IEnumerable ID)
{
var result=ids.AsParallel()
.选择(id=>GetOrder(id))
.ToList();
返回Json(结果);
}
订单GetOrder(int-id){…}
这是我在MVC3中运行一批异步操作时使用的模式:
public class MyController: AsyncController
{
public void IndexAsync(int[] ids)
{
var orders = new Orders[ids.Length];
AsyncManager.Parameters["orders"] = orders;
// tell the async manager there are X operations it needs to wait for
AsyncManager.OutstandingOperations.Increment(ids.Length);
for (int i = 0; i < ids.Length; i++){
var index = i; //<-- make sure we capture the value of i for the closure
// create the query
var query = _context.CreateQuery<Order>("Orders");
// run the operation async, supplying a completion routine
query.BeginExecute(ar => {
try {
orders[index] = query.EndExecute(ar).First(o => o.id == ids[index]);
}
catch (Exception ex){
// make sure we send the exception to the controller (in case we want to handle it)
AsyncManager.Sync(() => AsyncManager.Parameters["exception"] = ex);
}
// one more query has completed
AsyncManager.OutstandingOperations.Decrement();
}, null);
}
}
public ActionResult IndexCompleted(Order[] orders, Exception exception)
{
if (exception != null){
throw exception; // or whatever else you might like to do (log, etc)
}
return Json(orders);
}
private DataServiceContext _context; //let's ignore how this would be populated
}
公共类MyController:AsyncController
{
公共无效索引同步(int[]id)
{
var订单=新订单[ID.Length];
AsyncManager.Parameters[“orders”]=订单;
//告诉异步管理器需要等待X个操作
AsyncManager.OutstandingOperations.Increment(id.Length);
for(int i=0;io.id==ids[index]);
}
捕获(例外情况除外){
//确保我们将异常发送到控制器(以防我们要处理它)
AsyncManager.Sync(()=>AsyncManager.Parameters[“exception”]=ex);
}
//又完成了一个查询
AsyncManager.OutstandingOperations.Decrement();
},空);
}
}
public ActionResult IndexCompleted(订单[]订单,异常)
{
if(异常!=null){
抛出异常;//或您可能希望执行的任何其他操作(日志等)
}
返回Json(订单);
}
private DataServiceContext _context;//让我们忽略如何填充它
}
将运行一定数量的线程,但仍将阻止IIS线程。您是否在上下文的连接字符串上启用了?