multithreading - c# entity framework with threading -
i have run once off c# calculation on millions of rows of data , save results in table. haven't worked threading in c# in couple of years. i'm using .net v4.5 , ef v5.
the original code along lines of:
public static void main() { stopwatch sw = new stopwatch(); sw.start(); entities db = new entities(); docalc(db.clients.tolist()); sw.stop(); console.writeline(sw.elapsed); } private static void docalc(list<client> clients) { entities db = new entities(); foreach(var c in clients) { var transactions = db.gettransactions(c); var result = calulate(transactions); //the actual calc db.results.add(result); db.savechanges(); } }
here attempt @ multi-threading:
private static int numberofthreads = 15; public static void main() { stopwatch sw = new stopwatch(); sw.start(); entities db = new entities(); var splitupclients = splitupclients(db.clients()); task[] alltasks = new task[numberofthreads]; (int = 0; < numberofthreads; i++) { task task = task.factory.startnew(() => docalc(splitupclients[i])); alltasks[i] = task; } task.waitall(alltasks); sw.stop(); console.writeline(sw.elapsed); } private static void docalc(list<client> clients) { entities db = new entities(); foreach(var c in clients) { var transactions = db.gettransactions(c); var result = calulate(transactions); db.results.add(result); db.savechanges(); } } //splits list of clients n subgroups private static list<list<client>> splitupclients(list<client> clients) { int maxpergroup = (int)math.ceiling((double)clients.count() / numberofthreads); return ts.select((s, i) => new { str = s, index = }). groupby(o => o.index / maxpergroup, o => o.str). select(coll => coll.tolist()). tolist(); }
my question is:
is safe , correct way , there obvious shortcomings (especially regard ef)?
also, how find optimum number of threads? more merrier?
the entity framework dbcontext , objectcontext classes not thread-safe. should not use them on multiple threads.
although seems you're passing entities other threads, it's easy go wrong @ this, when lazy loading involved. means under covers entity callback context more data.
so instead, advice convert list of entities list of special immutable data structures need data needed calculation. immutable structures should not have call context , should not able change. when this, safe pass them on other threads calculation.
Comments
Post a Comment