Swift 命令行工具中的多个工作人员 [英] Multiple workers in Swift Command Line Tool
问题描述
在 Swift 中编写命令行工具 (CLT) 时,我想处理大量数据.我已经确定我的代码受 CPU 限制,性能可以从使用多核中受益.因此我想并行化部分代码.假设我想实现以下伪代码:
When writing a Command Line Tool (CLT) in Swift, I want to process a lot of data. I've determined that my code is CPU bound and performance could benefit from using multiple cores. Thus I want to parallelize parts of the code. Say I want to achieve the following pseudo-code:
Fetch items from database
Divide items in X chunks
Process chunks in parallel
Wait for chunks to finish
Do some other processing (single-thread)
现在我一直在使用 GCD,一个简单的方法看起来像这样:
Now I've been using GCD, and a naive approach would look like this:
let group = dispatch_group_create()
let queue = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT)
for chunk in chunks {
dispatch_group_async(group, queue) {
worker(chunk)
}
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
然而 GCD 需要一个运行循环,所以代码将挂起,因为该组永远不会被执行.runloop 可以用 dispatch_main()
启动,但它永远不会退出.也可以在几秒钟内运行 NSRunLoop
,但这并不是一个可靠的解决方案.不管 GCD,如何使用 Swift 来实现?
However GCD requires a run loop, so the code will hang as the group is never executed. The runloop can be started with dispatch_main()
, but it never exits. It is also possible to run the NSRunLoop
just a few seconds, however that doesn't feel like a solid solution. Regardless of GCD, how can this be achieved using Swift?
推荐答案
我错误地解释了挂起程序的锁定线程.这项工作将在没有运行循环的情况下正常执行.问题中的代码会运行良好,并阻塞主线程,直到整个组完成.
I mistakenly interpreted the locking thread for a hanging program. The work will execute just fine without a run loop. The code in the question will run fine, and blocking the main thread until the whole group has finished.
所以说 chunks
包含 4 项工作负载,以下代码启动 4 个并发工作线程,然后等待所有工作线程完成:
So say chunks
contains 4 items of workload, the following code spins up 4 concurrent workers, and then waits for all of the workers to finish:
let group = DispatchGroup()
let queue = DispatchQueue(label: "", attributes: .concurrent)
for chunk in chunk {
queue.async(group: group, execute: DispatchWorkItem() {
do_work(chunk)
})
}
_ = group.wait(timeout: .distantFuture)
这篇关于Swift 命令行工具中的多个工作人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!