简体   繁体   English

Task.detached 的默认优先级

[英]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.

相关问题 使用 Task.detached 是一种最佳实践和正确方法,以确保任务在非主线程中执行? - Is using Task.detached a best practice and correct approach, to make sure the Task is executed in non-main thread? 如何设置抛出错误的分离任务 - How to setup a detached task that throws an error Swfit:使用分离任务后如何返回主线程 - Swfit: How to return to main thread after using Detached Task 当操作具有 @MainActor 装饰器时,任务运行的优先级是多少? - What priority will as Task run as when the operation has the @MainActor decorator? MainActor.run 从分离的任务中调用时无法运行关闭 - MainActor.run failing to run closure when called from within a detached task CoreData:警告:无法为实体“任务”加载名为“任务”的类。 找不到类,而是使用默认的NSManagedObject - CoreData: warning: Unable to load class named 'Task' for entity 'Task'. Class not found, using default NSManagedObject instead NSPopover 以分离状态启动 - NSPopover to start in a detached state UIAlertController独立视图控制器 - UIAlertController Detached View Controller detached 和assignCurrentContext 是什么意思? - What do detached and assignCurrentContext meaning? 队列优先级似乎不正确 - Queue Priority seems not right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM