Skip to main content
Use `use` instead of `let` to make sure the stream is closed when the async read is completed.
Source Link

You can take advantage of async to do parallel IO which Async.Parallel makes very easy. Also, when you don't need the return value of an async call you can use Async.ignore in combination with do!: do! fs.AsyncRead(data, 0, data.Length) |> Async.ignore

However this specific snipper can be made much simpler since there is no reason to await the result of the computations when using Async.Parallel:

open System.IO

let fileContents = 
    [ "filename1"; "filename2"; "etc" ]
    |> List.map(fun file -> letasync { use fs = File.OpenRead(file) in return! fs.AsyncRead(fs.Length |> int)})
    |> Async.Parallel 
    |> Async.RunSynchronously
    |> Seq.map System.Text.Encoding.ASCII.GetString

One caveat - if the size of your file is more than 2GB it won't work due to long->int conversion - but that would be a crazy big file to just read the content of like that :-D

You can take advantage of async to do parallel IO which Async.Parallel makes very easy. Also, when you don't need the return value of an async call you can use Async.ignore in combination with do!: do! fs.AsyncRead(data, 0, data.Length) |> Async.ignore

However this specific snipper can be made much simpler since there is no reason to await the result of the computations when using Async.Parallel:

open System.IO

let fileContents = 
    [ "filename1"; "filename2"; "etc" ]
    |> List.map(fun file -> let fs = File.OpenRead(file) in fs.AsyncRead(fs.Length |> int))
    |> Async.Parallel 
    |> Async.RunSynchronously
    |> Seq.map System.Text.Encoding.ASCII.GetString

One caveat - if the size of your file is more than 2GB it won't work due to long->int conversion - but that would be a crazy big file to just read the content of like that :-D

You can take advantage of async to do parallel IO which Async.Parallel makes very easy. Also, when you don't need the return value of an async call you can use Async.ignore in combination with do!: do! fs.AsyncRead(data, 0, data.Length) |> Async.ignore

However this specific snipper can be made much simpler since there is no reason to await the result of the computations when using Async.Parallel:

open System.IO

let fileContents = 
    [ "filename1"; "filename2"; "etc" ]
    |> List.map(fun file -> async { use fs = File.OpenRead(file) in return! fs.AsyncRead(fs.Length |> int)})
    |> Async.Parallel 
    |> Async.RunSynchronously
    |> Seq.map System.Text.Encoding.ASCII.GetString

One caveat - if the size of your file is more than 2GB it won't work due to long->int conversion - but that would be a crazy big file to just read the content of like that :-D

Source Link

You can take advantage of async to do parallel IO which Async.Parallel makes very easy. Also, when you don't need the return value of an async call you can use Async.ignore in combination with do!: do! fs.AsyncRead(data, 0, data.Length) |> Async.ignore

However this specific snipper can be made much simpler since there is no reason to await the result of the computations when using Async.Parallel:

open System.IO

let fileContents = 
    [ "filename1"; "filename2"; "etc" ]
    |> List.map(fun file -> let fs = File.OpenRead(file) in fs.AsyncRead(fs.Length |> int))
    |> Async.Parallel 
    |> Async.RunSynchronously
    |> Seq.map System.Text.Encoding.ASCII.GetString

One caveat - if the size of your file is more than 2GB it won't work due to long->int conversion - but that would be a crazy big file to just read the content of like that :-D