I have function call
let result = session.Query<Domain.CustomerReadModel>().ToListAsync()
which returns Task<Collections.Generic.IReadOnlyList<Domain.CustomerReadModel>>
How do I "await" this task correctly in F#?
I tried
async {
session.Query<Domain.CustomerReadModel>().ToListAsync() |> ignore
}
as well as
let result = session.Query<Domain.CustomerReadModel>().ToListAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
This seems to compile but I can't use the result and return it:
session.Query<Domain.CustomerReadModel>().ToListAsync()
|> Async.AwaitTask
|> Async.RunSynchronously
Async.AwaitTask takes the task, wraps it in an async computation, and returns you that computation. Once you have it, you can use it with let! or do! or return! just like any other async computation.
async {
let queryAsFsharpAsync = session.Query<Domain.CustomerReadModel>().ToListAsync() |> Async.AwaitTask
let! result = queryAsFsharpAsync
...
}
Or without giving the computation its own name:
async {
let! result = session.Query<Domain.CustomerReadModel>().ToListAsync() |> Async.AwaitTask
...
}
Related
Not really sure why my code fails to compile, the error I am having:
Incomplete structured construct at or before this point in expression
let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
async {
let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
while message <> null do
let utf8Body = Encoding.UTF8.GetString(message.Body)
let error = JsonConvert.DeserializeObject<Error>(utf8Body)
let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
}
It seems to be related to my two async calls in the while loop, not really sure why though.
You can't end a block with a let!, there has to be a body after it; that's the reason for the syntax error.
The message you're defining on the last line is a different variable to the one tested in the while condition. It seems you want to mutate the message; for that, you have to explicitly create a mutable variable:
let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
async {
let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
let mutable message = msg
while message <> null do
let utf8Body = Encoding.UTF8.GetString(message.Body)
let error = JsonConvert.DeserializeObject<Error>(utf8Body)
let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
message <- msg
}
What is the idiomatic F# way of handling an asynchronous while loop accumulation?
I'm working with the new (still in preview) Azure Cosmos DB SDK. Querying the database returns a CosmosResultSetIterator<T> which has a HasMoreResults property and a FetchNextSetAsync() method. My straight-up translation of the C# code looks like this:
let private fetchItemsFromResultSet (resultSetIterator: CosmosResultSetIterator<'a>) =
let results = ResizeArray<'a>()
async {
while resultSetIterator.HasMoreResults do
let! response = resultSetIterator.FetchNextSetAsync() |> Async.AwaitTask
results.AddRange(response |> Seq.toArray)
return Seq.toList results
}
I would take a look at the AsyncSeq package. You can use it to create asynchronously computed sequences and then iterate them asynchronously or in parallel. This allows for the async-binding to be inside the sequence and the yield to occur asynchronously, so you don't have to build up an accumulator explicitly.
You can use it to do something like:
open FSharp.Control
let private fetchItemsFromResultSet (resultSetIterator: CosmosResultSetIterator<'a>) =
asyncSeq {
while resultSetIterator.HasMoreResults do
let! response = resultSetIterator.FetchNextSetAsync() |> Async.AwaitTask
yield! response |> AsyncSeq.ofSeq
}
IMHO tail-recursion is preferable to while loops as it's one way to avoid mutation.
For example:
let fetchItemsFromResultSet (resultSetIterator: CosmosResultSetIterator<'a>) =
let rec loop results =
async {
if resultSetIterator.HasMoreResults then
let! vs = resultSetIterator.FetchNextSetAsync () |> Async.AwaitTask
let vs = vs |> Seq.toList
return! loop (vs::results)
else
// List.rev needed because batches are in reverse
return results |> List.rev |> List.concat
}
loop []
Very recently, FSharp.Control.TaskSeq was added to support tasks natively with seqs. The answer here by #Just another metaprogrammer can be rewritten as
#r "nuget: FSharp.Control.TaskSeq"
open FSharp.Control
let private fetchItemsFromResultSet (resultSetIterator: CosmosResultSetIterator<'a>) = taskSeq {
while resultSetIterator.HasMoreResults do
let! response = resultSetIterator.FetchNextSetAsync()
yield! response |> TaskSeq.ofSeq
}
I defined await function like this:
let await (task : Task<'a>) =
task |> Async.AwaitTask |> Async.RunSynchronously
and two tasks like this:
let stringTask = new Task<string>(fun () -> "something")
let unitTask = new Task(fun () -> printf "something")
Calling them like this:
let stringResult = await stringTask
await unitTask
But the second is not generic and I can't call await for it, thus I edited the function argument like this:
let await (task : Task) =
task |> Async.AwaitTask |> Async.RunSynchronously
The problem is now the await stringTask is returning unit instead of string.
How should I write the await function to accept both Task<'a> and Task as the parameter and await it?
Your original definition for await is fine.
The problem is with unitTask which is not generic. Make it generic the same way you did with stringTask:
let unitTask = new Task<unit>(fun () -> printf "something")
If you want to handle both generic Task<'a> and non-generic Task, you have 2 options.
Create 2 await functions (the functional way):
let await (task : Task<'a>) = task |> Async.AwaitTask |> Async.RunSynchronously
let awaitUnit (task : Task ) = task |> Async.AwaitTask |> Async.RunSynchronously
or create 2 Await members for instance on type Task (the OO way):
type Task with
static member Await (task : Task ) = task |> Async.AwaitTask |> Async.RunSynchronously
static member Await (task : Task<'a>) = task |> Async.AwaitTask |> Async.RunSynchronously
By convention members use Pascal case so I used Await instead of await.
Remember to call task.Start() before you call your await.
Async.AwaitTask already has overloads for both Task<'a> and Task, so unless you really need a helper function, you can use it directly:
let stringResult = Async.AwaitTask stringTask |> Async.RunSynchronously
Async.AwaitTask unitTask |> Async.RunSynchronously
And if this is your actual code and you really need to block the thread to wait for those results, you can also just use the Task API directly to save the Task -> Async translation overhead:
let stringResult = stringTask.Result
unitTask.Wait()
You could create a Task -> Task<unit> helper function to convert Task objects you get from the API into Task<unit>. I suspect that this isn't the best way to do it, since it has been a while since I've paid much attention to the differences between F# async and the framework's Task-based asynchrony, but this works in a trivial extension of your example:
open System.Threading
open System.Threading.Tasks
let toUnitTask (t : Task) =
new Task<_>(fun () -> t.Start(); t |> Async.AwaitTask |> Async.RunSynchronously)
let await (task : Task<_>) =
task |> Async.AwaitTask |> Async.RunSynchronously
let stringTask = new Task<string>(fun () -> Thread.Sleep 3000; "something")
let unitTask = new Task(fun () -> Thread.Sleep 2000; printfn "something") |> toUnitTask
stringTask.Start()
unitTask.Start()
let stringResult = await stringTask
await unitTask
I have the following interface method:
Task<string[]> GetBlobsFromContainer(string containerName);
and its implementation in C#:
var container = await _containerClient.GetContainer(containerName);
var tasks = container.ListBlobs()
.Cast<CloudBlockBlob>()
.Select(b => b.DownloadTextAsync());
return await Task.WhenAll(tasks);
When I try to rewrite it in F#:
member this.GetBlobsFromContainer(containerName : string) : Task<string[]> =
let task = async {
let! container = containerClient.GetContainer(containerName) |> Async.AwaitTask
return container.ListBlobs()
|> Seq.cast<CloudBlockBlob>
|> Seq.map (fun b -> b.DownloadTextAsync())
|> ??
}
task |> ??
I'm stuck with the last lines.
How to return to Task<string[]> from F# properly?
I had to guess what the type of containerClient is and the closest I found is CloudBlobClient (which does not have getContainer: string -> Task<CloubBlobContainer> but it shouldn't be too hard to adapt). Then, your function might look like as follows:
open System
open System.Threading.Tasks
open Microsoft.WindowsAzure.Storage.Blob
open Microsoft.WindowsAzure.Storage
let containerClient : CloudBlobClient = null
let GetBlobsFromContainer(containerName : string) : Task<string[]> =
async {
let container = containerClient.GetContainerReference(containerName)
return! container.ListBlobs()
|> Seq.cast<CloudBlockBlob>
|> Seq.map (fun b -> b.DownloadTextAsync() |> Async.AwaitTask)
|> Async.Parallel
} |> Async.StartAsTask
I changed the return type to be Task<string[]> instead of Task<string seq> as I suppose you want to keep the interface. Otherwise, I'd suggest to get rid of the Task and use Async in F#-only code.
Will this work?
member this.GetBlobsFromContainer(containerName : string) : Task<string seq> =
let aMap f x = async {
let! a = x
return f a }
let task = async {
let! container = containerClient.GetContainer(containerName) |> Async.AwaitTask
return! container.ListBlobs()
|> Seq.cast<CloudBlockBlob>
|> Seq.map (fun b -> b.DownloadTextAsync() |> Async.AwaitTask)
|> Async.Parallel
|> aMap Array.toSeq
}
task |> Async.StartAsTask
I had to make some assumptions about containerClient etc. so I haven't been able to test this, but at least it compiles.
Suppose I have an async data source:
let getData() = async { return [ 3.14; 2.72 ] }
I could call it using let! and a temporary label:
let showData1() = async {
let! data = getData()
data
|> Seq.iter (printfn "%A")
}
Or, I could call it (inefficiently!) using Async.RunSynchronously and piping, and without a temporary label:
let showData2() = async {
getData()
|> Async.RunSynchronously
|> Seq.iter (printfn "%A")
}
I like the syntax of showData2 but know that calling Async.RunSynchronously ties up the calling thread.
Is there a syntax, operator, or function defined somewhere that allows me to pipe an Async<'T> into a function that accepts 'T?
It sounds like you want map for Async:
let mapAsync f a = async {
let! v = a
return (f v)
}
then you can do:
let m: Async<unit> = getData() |> mapAsync (Seq.iter (printfn "%A"))