在 .net webapi 中使用多线程
Use multithreading in .net webapi
在我的 dot net core web api 中,我知道可用的 cpu 线程数和要处理的数据数。假设我有 8 个线程用于 800 个数据更新请求。我该怎么做才能让每个线程并行处理 100 个数据更新请求?
(我是 .NET Core 多线程的新手,如果您不想共享代码,我想为初学者提供教程 link。)
这是我的代码:
[HttpPatch("UpdateExams")]
public async Task < ActionResult < ResponseDto2 >> UpdateExam([FromBody] List < Exam > input) {
// I know the number of threads -- lets say 8
int tc = Process.GetCurrentProcess().Threads.Count;
// I know the number of inputs -- lets say 800
int ic = input.Count;
// how can I distribute 100 data update request per core so that they run parallelly ?
// note: it is an data update operation
// below is my current api code
using
var transaction = _context.Database.BeginTransaction();
try {
foreach(var item in input) {
Exam exam = await _context.Exams.Where(i => i.FormNo == item.FormNo && i.RegNo == item.RegNo).FirstOrDefaultAsync();
if (exam != null) {
exam.ExamRollno = item.ExamRollno;
_context.Exams.Update(exam);
await _context.SaveChangesAsync();
}
}
transaction.Commit();
} catch (Exception e) {
return StatusCode(StatusCodes.Status500InternalServerError, new ResponseDto2 {
Message = "Something went wrong. Please try again later",
Success = false,
Payload = new {
e.StackTrace,
e.Message,
e.InnerException,
e.Source,
e.Data
}
});
}
return StatusCode(StatusCodes.Status200OK, new ResponseDto2 {
Message = "Data Updated successfully",
Success = true,
Payload = null
});
}
停止在线程中思考。当然,CPU 有“内核”和“线程”,但其中一些内核除了为您的应用程序提供服务外,更愿意做其他事情,例如保持 OS 功能并让您的 Web 服务器响应其他请求。
我可以 link 到 Task Parallel Library,但您评论说您不想要任何 MS 文档 link。
然后我将说明您实际在做什么:您正在使用 Entity Framework 进行批量导入并为每个实体调用 SaveChanges()。再多的并行化也无助于优化它。
有库可以在 SQL 中进行批量导入,而没有 Entity Framework 开销。
在我的 dot net core web api 中,我知道可用的 cpu 线程数和要处理的数据数。假设我有 8 个线程用于 800 个数据更新请求。我该怎么做才能让每个线程并行处理 100 个数据更新请求?
(我是 .NET Core 多线程的新手,如果您不想共享代码,我想为初学者提供教程 link。)
这是我的代码:
[HttpPatch("UpdateExams")]
public async Task < ActionResult < ResponseDto2 >> UpdateExam([FromBody] List < Exam > input) {
// I know the number of threads -- lets say 8
int tc = Process.GetCurrentProcess().Threads.Count;
// I know the number of inputs -- lets say 800
int ic = input.Count;
// how can I distribute 100 data update request per core so that they run parallelly ?
// note: it is an data update operation
// below is my current api code
using
var transaction = _context.Database.BeginTransaction();
try {
foreach(var item in input) {
Exam exam = await _context.Exams.Where(i => i.FormNo == item.FormNo && i.RegNo == item.RegNo).FirstOrDefaultAsync();
if (exam != null) {
exam.ExamRollno = item.ExamRollno;
_context.Exams.Update(exam);
await _context.SaveChangesAsync();
}
}
transaction.Commit();
} catch (Exception e) {
return StatusCode(StatusCodes.Status500InternalServerError, new ResponseDto2 {
Message = "Something went wrong. Please try again later",
Success = false,
Payload = new {
e.StackTrace,
e.Message,
e.InnerException,
e.Source,
e.Data
}
});
}
return StatusCode(StatusCodes.Status200OK, new ResponseDto2 {
Message = "Data Updated successfully",
Success = true,
Payload = null
});
}
停止在线程中思考。当然,CPU 有“内核”和“线程”,但其中一些内核除了为您的应用程序提供服务外,更愿意做其他事情,例如保持 OS 功能并让您的 Web 服务器响应其他请求。
我可以 link 到 Task Parallel Library,但您评论说您不想要任何 MS 文档 link。
然后我将说明您实际在做什么:您正在使用 Entity Framework 进行批量导入并为每个实体调用 SaveChanges()。再多的并行化也无助于优化它。
有库可以在 SQL 中进行批量导入,而没有 Entity Framework 开销。