当前位置:网站首页>Chapter 14 signals (IV) - examples of multi process tasks

Chapter 14 signals (IV) - examples of multi process tasks

2022-07-01 12:21:00 yaoxin521123

Chapter 14 The signal ( Four )- Multi process task example

According to this idea, multi task start-up query and summary data can be carried out .

principle

  • utilize job The mechanism starts the background process .
  • utilize loop The number of cycle reducing processes is equal to the number of open processes , Determine whether the multi process task is completed .
  1. Create a table and insert 1000W Data , Statistics Moeny Total field amount

 Insert picture description here

  1. establish demo The code is as follows .
Class Demo.SemaphoreDemo Extends %RegisteredObject
{

///  Do ##class(Demo.SemaphoreDemo).Sample(5)
ClassMethod Sample(pJobCount = 3)
{
	k ^yx("Amt"),^yxAmt
	
	 /* 1. Start signal  */
    s mSem = ##class(Demo.Sem).%New()
    If ('($isobject(mSem))) 
    {
		q " Boot failure "
    }
    
    /* 2.  The initialization semaphore is 0 */
    d mSem.Init(0)
    
    s t1 = $zh
    
    /* 3.  By specified quantity , Start background tasks  */
    for i = 1 : 1 : pJobCount
    {
        j ..Task(i)
    }
    
    w " start-up job Time :"_ ($zh - t1),!
	
    /* 4.  Wait for the background task to complete  */
    s tCount = i,tSC = 0
    /*  The judgment condition for the completion of background tasks is : Reduced semaphore = Total background tasks  */
    while (tSC < tCount)
    {
        s tSC = tSC + mSem.Decrement(tCount, 10)
    }
    
    w " Completion time :"_ ($zh - t1),!
    
    
	s moneyAmt = 0

		
	s data = ""
	for {
		s data = $o(^yxAmt(data))
		q:(data = "")
		s moneyAmt = moneyAmt + ^yxAmt(data)
	}
	d mSem.Delete()
    w " Total sum " _ moneyAmt,!
    
    w " Summary time :"_ ($zh - t1),!
	q
}

ClassMethod Task(i)
{
    s tSem = ##class(Demo.Sem).%New()
	s moneyAmt = 0
	for j = (i * 100000) + 1 : 1 : (i + 1) * 100000 {
		s money = $li(^M.YxPersonD(j), 3)
		s moneyAmt = moneyAmt + money
	}
	s ^yxAmt("moneyAmt" _ i) = moneyAmt
	s ^yx("Amt") = $i(^yx("Amt"))
    d tSem.Open(##class(Demo.Sem).Name())
    d tSem.Increment(1)
    d tSem.%Close()
	q moneyAmt
}
}
  1. Create a signal class , Definition name And initialization signal method .
Class Demo.Sem Extends %SYSTEM.Semaphore
{

ClassMethod Name() As %String
{
    q "Semaphore"
}

Method Init(initvalue = 0) As %Status
{
	try {
		If (..Create(..Name(), initvalue)) {
			ret 1
		} else {
			ret 0
		}
	} catch {
		ret 0
	}
}

}
  1. call
DHC-APP>Do ##class(Demo.SemaphoreDemo).Sample(5)
 start-up job Time :.098982
 Completion time :.119744
 Total sum 250088825096472
 Summary time :.119774
原网站

版权声明
本文为[yaoxin521123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011214463957.html