Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
...
// хеш товар-количество
var articles = income.getArticles();
//оприходовали товар
SaveIncome(income);
// так делать не надо, но сейчас не скорость важна
foreach(a in articles)
{
var outcome = FindOutComeByArticle(a);
// резервируем товар a
SaveOutcome(outcome, a);
}
...
...
// хеш товар-количество
var articles = income.getArticles();
//оприходовали товар
SaveIncome(income);
// ускоряем в 1000 раз !
RunParallel(articles, (a) => var outcome = FindOutComeByArticle(a);
// резервируем товар a
SaveOutcome(outcome, a) );
...
Во-вторых, такие ошибки крайне сложно искать — они недетерминированны и могут не воспроизводится под дебагером, да и вообще требовать весьма специфических условий для воспроизведения (например на одном серваке есть ошибка, на другом нет).
#Region PublicInterface
// Parameters
// size - integer - count of process
// comm - string - module and procedure name
// param - structure - structure of parameters of the procedure
//
Procedure Init(size, comm, param = Undefined) Export
CommParam = new Structure;
CommParam.Insert("size", size);
CommParam.Insert("comm", comm);
JobParam = new Array;
JobParam.Add(CommParam);
JobParam.Add(param);
For i = 1 to size Do
commParam.Insert("rank", i);
BackgroundJobs.Execute(comm, JobParam, i);
EndDo
EndProcedure
// Parameters
// comm - string - module and procedure name
// rank - integer - key of the process if Undefined wait all process in comm
//
Procedure Wait(comm, rank = Undefined) Export
Selection = New Structure("MethodName, State", comm, BackgroundJobState.Active);
If rank <> Undefined Then
Selection.Insert("Key", rank);
EndIf;
ArrayOfJobs = BackgroundJobs.GetBackgroundJobs(Selection);
BackgroundJobs.WaitForCompletion(ArrayOfJobs);
EndProcedure
// Parameters
// buf - any - module and procedure name
// dest - integer - key of the destination process
// msgtag - integer - tag to devide messege from one sourse
// comm - structure - construct in Init
//
Procedure Send(buf, dest, msgtag, comm) Export
Param = new Structure;
Param.Insert("buf", buf);
Param.Insert("dest", dest);
Param.Insert("source", comm.rank);
Param.Insert("msgtag", msgtag);
Param.Insert("comm", comm.comm);
Message = new UserMessage;
Message.Text = ValueToStringInternal(param);
Message.Message();
EndProcedure
// Parameters
// source - integer - key of the source process
// msgtag - integer - tag to devide messege from one sourse
// comm - structure - construct in Init
//
// Return
// any
//
Function Recv(source, msgtag, comm) Export
Selection = New Structure("MethodName, Key", comm.comm, source);
ArrayOfJobs = BackgroundJobs.GetBackgroundJobs(Selection);
If ArrayOfJobs.Count() < 1 Then
Return Undefined;
EndIf;
Job = ArrayOfJobs[ArrayOfJobs.Count() - 1];
While True Do
Messages = Job.GetUserMessages();
Count = Messages.Count() - 1;
For i = 0 to Count Do
Value = ValueFromStringInternal(Messages[i].Text);
If Value.dest = comm.rank
And Value.source = source
And Value.comm = comm.comm
And Value.msgtag = msgtag Then
Return Value.buf;
EndIf
EndDo;
EndDo;
EndFunction
#EndRegion
Применение параллельных алгоритмов в среде 1С Предприятия