我试图找出等待一些 I/O 完成端口完成的最佳方法。
对于这种情况,假设我在 MVC3 Web 应用程序中。(我的理解是这里推荐使用 I/O Completion 端口,这样我就可以将原来的线程返回给 IIS 来服务其他请求)
假设我有一个 ID 数组,我想从某个网络调用中为每个 ID 获取一个对象。
并行化这种同步方法的最佳方法是什么?
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
}
我知道它会这样开始:
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
}
我应该使用DataServiceContext.BeginExecute方法吗? DataServiceContext.BeginExecuteBatch?我正在使用的数据服务一次只能获取一条记录(这超出了我的控制范围),我希望这些单独的查询并行运行。