or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Swift Concurrency Part 1
Swift Concurrency Manifesto
Callback hell
Term
async/await
Continuations
Task
Task {}
Task.detached {}
TaskGroup
Actor
actor
GlobalActor
@globalActor
@MainActor
TaskLocal
AsyncSequence
AsyncStream
Job 相關
Concurrency vs. Parallelism
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →Concurrency
Making progress on more than one task seemingly at the same time.
Concurrency
Parallel Execution
Making progress on more than one task at the exact same time.
Parallel Execution
async/await
宣告
一般使用方式
在 sync func 內使用
get async throws
closure
隱式 closure
對現有 api 的衝擊
對現有 api 的衝擊(rule)
對現有 api 的衝擊(rule)
自動隱式轉換
可以用同步函數替代非同步
Test
限制
限制
限制
同步函數 不能呼叫 異步函數
同步函數 vs 異步函數
同步函數
異步函數
同步函數
同步函數
異步函數
異步函數
暫停點(Suspension point)
顯試操作
await
原子性
thread
暫停點
CheckedContinuation
Converting closure-based code into async/await in Swift
async code
callback code
Continuations code
Explicit continuations
withCheckedContinuation
CheckedContinuation<T, Never>
withCheckedThrowingContinuation
CheckedContinuation<T, Error>
使用 Continuation 封裝 callback code
注意事項
continuation.resume(returning: value)
continuation.resume(throwing: error)
Rx VS Continuation
Rx
Continuation
Demo
UnsafeContinuation
跟
CheckedContinuation
擁有一樣的介面可直接切換
無 run time check
Task
A unit of asynchronous work.
一個非同步工作的單元
When you create an instance of
Task
,you provide a closure that contains the work for that task to perform.
當你建立
Task
實體時,你需要提供 closure 以供 task 執行Tasks can start running immediately after creation;
任務可以在創建後立即開始運行
you don't explicitly start or schedule them.
您不用明確啟動或安排它們。
After creating a task, you use the instance to interact with it
for example, to wait for it to complete or to cancel it.
創建任務後,您使用實例與其交互
例如,等待它完成或取消它。
等待它完成
取消它
It's not a programming error to discard a reference to a task
without waiting for that task to finish or canceling it.
丟棄對任務的引用不是編程錯誤
無需等待該任務完成或取消它。
A task runs regardless of whether you keep a reference to it.
無論您是否保留對它的引用,任務都會運行。
However, if you discard the reference to a task,
you give up the ability
to wait for that task's result or cancel the task.
但是,如果您放棄持有
task
你會失去等待該任務的結果或取消該任務之能力
Only code that's running as part of the task can interact with that task.
To interact with the current task,
you call one of the static methods on
Task
.只有運行在
task
內的 code可以用
static method
跟當前 task
互動Task Cancellation
Tasks include a shared mechanism for indicating cancellation,
but not a shared implementation for how to handle cancellation.
任務包括指示取消的共享機制,但不包括如何處理取消的共享實現。
Depending on the work you're doing in the task,
the correct way to stop that work varies.
根據您在任務中所做的工作,停止該工作的正確方法會有所不同。
Likewise,
it's the responsibility of the code running as part of the task
to check for cancellation whenever stopping is appropriate.
同樣,作為任務的一部分運行的代碼有責任在適當的停止時檢查取消。
In a long-task that includes multiple pieces,
you might need to check for cancellation at several points,
and handle cancellation differently at each point.
在包含多個部分的長任務中,您可能需要在多個點檢查取消,並在每個點以不同的方式處理取消。
If you only need to throw an error to stop the work,
call the
Task.checkCancellation()
function to check for cancellation.如果只需要拋出一個錯誤來停止工作,調用
Task.checkCancellation()
函數來檢查是否取消。Other responses to cancellation include
returning the work completed so far, returning an empty result, or returning
nil
.對取消的其他響應包括返回到目前為止已完成的工作、返回空結果或返回“nil”。
Cancellation is a purely Boolean state;
there's no way to include additional information
like the reason for cancellation.
取消是一個純粹的
bool
狀態; 無法包含其他信息,例如取消原因。This reflects the fact that a task can be canceled for many reasons,
and additional reasons can accrue during the cancellation process.
這反映了一個任務可以由於多種原因被取消的事實,並且在取消過程中可能會產生其他原因。
Demo
Job
A task's execution can be seen as a series of periods where the task ran.
Each such period ends at a suspension point or the
completion of the task.
運行中 task 可以被視為一連串的執行週期(series of periods)
這些週期的結束點位於
These periods of execution are represented by instances of
PartialAsyncTask
.Unless you're implementing a custom executor,
you don't directly interact with partial tasks.
這些執行週期可以
PartialAsyncTask
的實例呈現除非你要實作 custom executor
否則你不必直接跟它做互動
UnownedJob
PartialAsyncTask
已改名為UnownedJob
Unstructured Concurrency
Unlike tasks that are part of a task group, an unstructured task doesn’t have a parent task.
Attached Task
To create an unstructured task that runs on the current actor, call the Task.init(priority:operation:) initializer.
Detached Task
To create an unstructured task that’s not part of the current actor, known more specifically as a detached task, call the Task.detached(priority:operation:) class method.
Task 小結
Task 建立後會自行執行
cancel 只能停止
尚未開始的 TaskTask 的 cancel 流程必須自行掌控
一個 Task 可以拆分成多個 UnownedJob
Actor Model
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →信箱
擁有復數地址
地址 != ID
沒有 ID
允許操作
子 actor
ISOLATED
狀態
各語言實作方式不同
Swift Actor
protocol Actor
UnownedSerialExecutor
SerialExecutor
UnownedJob
UnownedJob
Actor reentrancy
isolated functions
是可重入的(reentrant).isolated functions
暫停時, 重入性會允許在原 isolated function 恢復之前,並在 actor 先執行其他工作,稱之interleaving
.Actor reentrancy
預期是 before before after after
Actor reentrancy(預期)
結論(坑)
async func 有機會排進
actor's queue
Actor 猜想
Thread
Thread
Thread
Thread
Thread
Thread 2 Queue : com.apple.root.default-qos.cooperative (concurrent)
Queue
Queue
Queue
Executor
UnownedSerialExecutor
問題 No.1
問題 No.2
坑 No.1
Task 有/無 mutable var
坑 No.2
async func 有/無 參數
Q & A
??? No.1
0296-async-await.md
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →async function
跟特定 actor
相關結論
一般 async func 有可能會延續上一個 actor嗎?
??? NO.2
swift.org
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →跑在
目前的 actor
Reference