当前位置:网站首页>第十四章 信号(四)- 多进程任务示例
第十四章 信号(四)- 多进程任务示例
2022-07-01 12:14:00 【yaoxin521123】
第十四章 信号(四)- 多进程任务示例
可根据此思想进行多任务启动查询汇总数据。
原理
- 利用
job机制开启后台进程。 - 利用
loop循环减少进程的数量等于开启进程的数量,判断多进程任务是否完成。
- 创建表并插入
1000W条数据,统计Moeny字段总金额

- 创建
demo代码如下。
Class Demo.SemaphoreDemo Extends %RegisteredObject
{
/// Do ##class(Demo.SemaphoreDemo).Sample(5)
ClassMethod Sample(pJobCount = 3)
{
k ^yx("Amt"),^yxAmt
/* 1.启动信号 */
s mSem = ##class(Demo.Sem).%New()
If ('($isobject(mSem)))
{
q "启动失败"
}
/* 2. 初始化信号量为0 */
d mSem.Init(0)
s t1 = $zh
/* 3. 按指定数量,启动后台任务 */
for i = 1 : 1 : pJobCount
{
j ..Task(i)
}
w "启动job时间:"_ ($zh - t1),!
/* 4. 等待后台任务完成 */
s tCount = i,tSC = 0
/* 后台任务完成的判断条件是:减少的信号量=总后台任务数 */
while (tSC < tCount)
{
s tSC = tSC + mSem.Decrement(tCount, 10)
}
w "完成时间:"_ ($zh - t1),!
s moneyAmt = 0
s data = ""
for {
s data = $o(^yxAmt(data))
q:(data = "")
s moneyAmt = moneyAmt + ^yxAmt(data)
}
d mSem.Delete()
w "总金额" _ moneyAmt,!
w "汇总时间:"_ ($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
}
}
- 创建信号类,定义
name和初始化信号方法。
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
}
}
}
- 调用
DHC-APP>Do ##class(Demo.SemaphoreDemo).Sample(5)
启动job时间:.098982
完成时间:.119744
总金额250088825096472
汇总时间:.119774
边栏推荐
- 栈的应用——括号匹配问题
- LeetCode力扣(剑指offer 31-35)31. 栈的压入弹出序列32I.II.III.从上到下打印二叉树33. 二叉搜索树的后序遍历序列34. 二叉树中和为某一值的路径35. 复杂链表的复制
- 【datawhale202206】pyTorch推荐系统:精排模型 DeepFM&DIN
- Redis' attack tactics
- Indefinite integral
- [Suanli network] technological innovation of Suanli Network -- key technology of operation service
- [Yu Yue education] financial management reference materials of Ningbo University of Finance and Economics
- Golang根据参数引入相应配置文件的实现方式
- Acly and metabolic diseases
- [JS advanced] promise explanation
猜你喜欢

C summary of knowledge points 1

Interpretation of R & D effectiveness measurement framework
![[datawhale202206] pytorch recommendation system: precision model deepfm & DIN](/img/4f/8799016731a2c1647b6f2f4d96b754.png)
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN

On recursion and Fibonacci sequence

easyexcel的使用

Mechanism and type of CPU context switch

循环链表--

Use of easyexcel

How to use opcache, an optimization acceleration component of PHP

91. (cesium chapter) cesium rocket launch simulation
随机推荐
Golang des-cbc
Onenet Internet of things platform - the console sends commands to mqtt product devices
二叉树的链式存储
Summary of JFrame knowledge points 1
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 4
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
ASTM D 3801固体塑料垂直燃烧试验
(混更一篇)多个txt文本转一个表格
队列的链式存储
Message queue monitoring refund task batch process
Talk about the pessimistic strategy that triggers full GC?
Pandas reads MySQL data
Istio, ebpf and rsocket Broker: in depth study of service grid
usb peripheral 驱动 - cable connect/disconnect
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
GPS 数据中的精度因子(DOP)与协方差之间的关系 (参考链接)
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 3
C knowledge point form summary 2
LeetCode 454. 四数相加 II
How to install php7 and perform performance test using yum