Async.StartChild<'T> 메서드(F#)
비동기 워크플로 내에서 자식 계산을 시작합니다. 이렇게 하면 여러 비동기 계산을 동시에 실행할 수 있습니다.
네임스페이스/모듈 경로: Microsoft.FSharp.Control
어셈블리: FSharp.Core(FSharp.Core.dll)
// Signature:
static member StartChild : Async<'T> * ?int -> Async<Async<'T>>
// Usage:
Async.StartChild (computation)
Async.StartChild (computation, millisecondsTimeout = millisecondsTimeout)
매개 변수
computation
형식: Async<'T>자식 계산입니다.
millisecondsTimeout
형식: int제한 시간 값(밀리초)입니다. 이 값을 지정하지 않으면 Infinite에 해당하는 기본값 -1이 지정됩니다.
반환 값
입력 계산이 완료될 때까지 대기하는 새 계산입니다.
설명
이 메서드는 일반적으로 다음과 같이 F# 비동기 워크플로에서 let! 바인딩의 바로 오른쪽에 사용해야 합니다.
async {
...
let! completor1 = childComputation1
|> Async.StartChild
let! completor2 = childComputation2
|> Async.StartChild
...
let! result1 = completor1
let! result2 = completor2
... }
이 방식으로 사용하면 StartChild를 사용할 때마다 childComputation의 인스턴스가 시작되고 연산이 완료될 때까지 대기할 계산을 나타내는 completor 개체가 반환됩니다. 실행 시 completor는 childComputation이 완료될 때까지 대기합니다.
예제
다음 코드 예제에서는 Async.StartChild를 사용하는 방법을 보여 줍니다.
open System.Windows.Forms
let bufferData = Array.zeroCreate<byte> 100000000
let asyncChild filename =
async {
printfn "Child job start: %s" filename
use outputFile = System.IO.File.Create(filename)
do! outputFile.AsyncWrite(bufferData)
printfn "Child job end: %s " filename
}
let asyncParent =
async {
printfn "Parent job start."
let! childAsync1 = Async.StartChild(asyncChild "longoutput1.dat")
let! childAsync2 = Async.StartChild(asyncChild "longoutput2.dat")
let! result1 = childAsync1
let! result2 = childAsync2
printfn "Parent job end."
}
let form = new Form(Text = "Test Form")
let button = new Button(Text = "Start")
form.Controls.Add(button)
button.Click.Add(fun args -> Async.Start(asyncParent)
printfn "Completed execution." )
Application.Run(form)
샘플 출력
작업이 동시에 실행되고 있으므로 출력이 인터리브됩니다.
플랫폼
Windows Windows 서버 2012, Windows Server 2008 R2, Windows 7, 8
버전 정보
F# 코어 라이브러리 버전
지원: 2.0, 4.0, 노트북