Assuming asyncSendMsg
doesn't return anything and I want to start it inside another async block, but not wait for it to finish, is there any difference between this:
async {
//(...async stuff...)
for msg in msgs do
asyncSendMsg msg |> Async.Start
//(...more async stuff...)
}
and
async {
//(...async stuff...)
for msg in msgs do
let! child = asyncSendMsg msg |> Async.StartChild
()
//(...more async stuff...)
}
The key difference is that when you start a workflow with
Async.StartChild
, it will share the cancellation token with the parent. If you cancel the parent, all children will be cancelled too. If you start the child usingAsync.Start
, then it is a completely independent workflow.Here is a minimal example that demonstrates the difference:
In this example, if you start the computation using
(1)
, then all work items will finish and print after 2 seconds. If you use(2)
they will all be cancelled when the main workflow is cancelled.