【发布时间】:2022-01-14 05:26:26
【问题描述】:
使用 Swift 的新 async/await 功能,我想模拟串行队列的调度行为(类似于过去使用 DispatchQueue 或 OperationQueue 的方式)。
稍微简化一下我的用例,我有一系列异步任务,我想从调用站点触发并在它们完成时获得回调,但根据设计我想一次只执行一个任务(每个任务取决于上一个任务的完成情况)。
今天,这是通过将Operations 与maxConcurrentOperationCount = 1 放在OperationQueue 上来实现的,并在适当的时候使用Operation 的依赖功能。我已经使用 await withCheckedContinuation 围绕现有的基于闭包的入口点构建了一个 async/await 包装器,但我正试图弄清楚如何将整个方法迁移到新系统。
这可能吗?这是否有意义,还是我从根本上违背了新的 async/await 并发系统的意图?
我已经研究了一些使用 Actors 的方法,但据我所知,没有办法真正强制/期望使用这种方法进行串行执行。
--
更多上下文 - 这包含在网络库中,今天的每个操作都是针对新请求的。 Operation 进行一些请求预处理(考虑身份验证/令牌刷新,如果适用),然后触发请求并继续下一个 Operation,从而避免在不需要时重复身份验证预处理。从技术上讲,每个 Operation 并不知道它依赖于先前的操作,但 OperationQueue 的调度强制执行串行执行。
在下面添加示例代码:
// Old entry point
func execute(request: CustomRequestType, completion: ((Result<CustomResponseType, Error>) -> Void)? = nil) {
let operation = BlockOperation() {
// do preprocessing and ultimately generate a URLRequest
// We have a URLSession instance reference in this context called session
let dataTask = session.dataTask(with: urlRequest) { data, urlResponse, error in
completion?(/* Call to a function which processes the response and creates the Result type */)
dataTask.resume()
}
// queue is an OperationQueue with maxConcurrentOperationCount = 1 defined elsewhere
queue.addOperation(operation)
}
// New entry point which currently just wraps the old entry point
func execute(request: CustomRequestType) async -> Result<CustomResponseType, Error> {
await withCheckedContinuation { continuation in
execute(request: request) { (result: Result<CustomResponseType, Error>) in
continuation.resume(returning: result)
}
}
}
【问题讨论】:
-
“我想从呼叫站点启动并在他们完成时获得回调”不。没有回调。这才是重点。从你的想法中抹去这个概念。演员就是一个上下文序列化器,所以请展示你的代码不能完成这个。一旦你说
await,在异步材料完成之前你无法继续,那么问题是什么?在没有回调的情况下理顺这些东西,这正是 async/await 所做的。 -
回调是一个糟糕的词选择,我已经编写的包装器是一个异步函数,它返回一个结果(替换旧的闭包/回调入口点)——我只是想使实现现代化里面。
-
正如我所说,您应该提供一些代码。你要求做的事情听起来很简单,所以看看为什么不是这样会很有用。例如,我轻松编写了一个绘制 Mandelbrot 集的演示示例,在现有重绘完成之前,您甚至无法开始重绘该集;多亏了一位演员,他们完全按照您的建议顺序排列。所以请说明为什么这对你不起作用。
-
是的,只要演员中没有代码说等待。一旦它这么说,演员就变成了可重入的。
-
如果这是问题所在,我会说这是 stackoverflow.com/questions/68686601/… 的副本。我只是从那里重复我的答案。
标签: swift async-await grand-central-dispatch nsoperationqueue