[英]Default priority of Task.detached
When you create a detached task with detached(priority:operation:) but leave priority
set to nil, what priority does iOS assign?当您使用detached(priority:operation:)创建一个分离任务但将
priority
设置为 nil 时,iOS 分配什么优先级?
For example, suppose a photo output handler calls an actor like this:例如,假设照片 output 处理程序调用这样的演员:
class PhotoViewController: UIViewController {
func photoOutput(_ photoOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let photoWithContext = someCallToMainActor(photo)
Task.detached {
let result = async someCallToAnotherActor(photoWithContext)
async anotherCallToMainActor(result)
}
}
}
The docs for detached(priority:operation:) say about priority, “You need to handle these considerations manually with a detached task.” detached(priority:operation:) 的文档提到了优先级,“你需要通过一个分离的任务手动处理这些考虑。” Since the detached task isn't a child task, I don't get the feature that, “Child tasks inherit the parent task's priority”.
由于分离的任务不是子任务,我没有得到“子任务继承父任务的优先级”的功能。
I realize that I can specify the priority explicitly.我意识到我可以明确指定优先级。 Still, there presumably is a reason that detached(priority:operation:) allows priority to be nil, and one would expect a well defined semantic.
尽管如此, detached(priority:operation:) 允许优先级为零可能是有原因的,并且人们会期望一个定义良好的语义。 Are there any rules that govern the detached task's priority?
是否有任何规则来管理分离任务的优先级?
For child tasks the priority of task is inherited from parent task, and for both detached task and a parent task if priority provided is nil
, TaskPriority.medium
is used as priority.对于子任务,任务的优先级是从父任务继承的,对于分离任务和父任务,如果提供的优先级为
nil
,则使用TaskPriority.medium
作为优先级。
TL&DR; TL&DR; Detached tasks with no specified priority end up having the
.medium
priority, at least this holds true at the time of writing this answer.没有指定优先级的分离任务最终具有
.medium
优先级,至少在编写此答案时这是正确的。 Things can change in the future, however not sure how likely is for this one to change.未来可能会发生变化,但不确定这一变化的可能性有多大。
One of the many nice things about Swift is that it's open source, so we can take a look and see what happens under the hood. Swift 的众多优点之一是它是开源的,因此我们可以看看引擎盖下发生了什么。
If we take a look at the Task.detached() implementation we can see the following code:如果我们看一下Task.detached()实现,我们可以看到以下代码:
public static func detached(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> Success
) -> Task<Success, Failure> {
#if compiler(>=5.5) && $BuiltinCreateAsyncTaskInGroup
// Set up the job flags for a new task.
let flags = taskCreateFlags(
priority: priority, isChildTask: false, copyTaskLocals: false,
inheritContext: false, enqueueJob: true,
addPendingGroupTaskUnconditionally: false)
The priority gets passed to the taskCreateFlags() function, which currently looks like this:优先级被传递给taskCreateFlags() function,目前看起来像这样:
func taskCreateFlags(
priority: TaskPriority?, isChildTask: Bool, copyTaskLocals: Bool,
inheritContext: Bool, enqueueJob: Bool,
addPendingGroupTaskUnconditionally: Bool
) -> Int {
var bits = 0
bits |= (bits & ~0xFF) | Int(priority?.rawValue ?? 0)
...
}
So, in case the priority
argument is nil, the first byte of the task flags, which represent the priority, will be zero.因此,如果
priority
参数为 nil,则表示优先级的任务标志的第一个字节将为零。
Now, if we continue digging, we reach the compiler area, and at some point we'll reach to Task.cpp:755 , which reads现在,如果我们继续挖掘,我们会到达编译器区域,在某个时候我们会到达Task.cpp:755 ,它的内容如下
if (isUnspecified(basePriority)) {
basePriority = JobPriority::Default;
}
, and so happens that JobPriority::Default corresponds to the value 0x15
, which, coincidentally or not, and as others have noticed, correspond to the .medium
task priority. ,因此JobPriority::Default对应于值
0x15
,无论是否巧合,正如其他人所注意到的那样,它对应于.medium
任务优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.