custom async primitive function in f# using Async.FromContinuations - asynchronous

I'm a bit newbie with the async workflow and I think it has somethings I am not understand so well...I'm following the book Real world functional programming and in it writes the async primitive in this way
let Sleep(time) =
Async.FromContinuations(fun (cont, econt, ccont) ->
let tmr = new System.Timers.Timer(time, AutoReset = false)
tmr.Elapsed.Add(fun _ -> cont())
tmr.Start()
);;
I've tried write my own async primitive using a very slow factorial implementation, the code is it:
let rec factorial(n : int) (mem : bigint) =
match n with
| 0 | 1 -> printfn "%A" mem
| _ -> factorial (n - 1) (mem * bigint(n))
let BigFactorial(numero,mesaje)=
Async.FromContinuations(fun (cont,error,cancelation) ->
printfn "begin number: %s" mesaje
factorial numero 1I |>ignore
printfn "End number: %s ." mesaje
cont())
now I wrote this
Async.RunSynchronously(async{
printfn "Start!!..."
do! BigFactorial(30000,"30M")
do! BigFactorial(10000, "10M")
printfn "End!!..."
})
but it isn't running in async way precisely....
Start!!...
begin number: 30M
2759537246219...(A nice long number!)
end number: 30M .
begin number: 10M .
2846259680917054518906...(other nice number)
End!!...
if I execute it in parallel then works good...
let task1 = async{
printfn "begin"
do! BigFactorial(30000,"30")
printfn "end..."
}
let task2 = async{
printfn "begin"
do! BigFactorial(10000,"10")
printfn "end!!..."
}
Async.RunSynchronously(Async.Parallel[task1;task2])
begin
begin: 30
begin
begin: 10M
//long numbers here
end....
end.....
but I wish know the reason (or the several reasons!) why the first code doesn't work...
thanks so much I appreciate any help...

As with all async bindings, do! runs on the current thread, and thus your two sequential do!s run sequentially.
If you want them to run in parallel, you must say so explicitly:
async {
printfn "Start!!..."
let! tokenA = BigFactorial (30000, "30M") |> Async.StartChild // on new thread
let! tokenB = BigFactorial (10000, "10M") |> Async.StartChild // on new thread
do! tokenA // block until `BigFactorial (30000, "30M")` finishes
do! tokenB // block until `BigFactorial (10000, "10M")` finishes
printfn "End!!..." }
|> Async.RunSynchronously

Related

In F# how do I create a list of Async<unit> inside an async computation without mutable collection?

I have a small program using the producer/consumer pattern in F#. I want to start 1 producer, then 8 consumers, wait for the producer then wait for the consumers and end the program.
The code that sets this in motion is:
async {
let! p = Async.StartChild producer
let maxConcurrent = 8
let consumers = List<Async<unit>>()
for _ in 0 .. maxConcurrent - 1 do
let! c = Async.StartChild consumer
consumers.Add(c)
do! p
for i in 0 .. maxConcurrent - 1 do
do! consumers[i]
}
|> Async.RunSynchronously
While this works it feels wrong to use the mutable List from System.Collections.Generic instead of just F# list. But I cannot generate the list of Async needed using List. I have tried:
let consumers2 = List.init 8 (fun _ -> async {
let! c = Async.StartChild consumer
return c
})
But this now wraps it in async again, so it becomes a list<async<async<unit>>>, if it's instead return! then the app hangs (I'm guessing because I'm not awaiting the task that should start the Task instead awaiting also the task that awaits finalization)
Full code is available here
I think Async.Parallel will flatten the asyncs the way you want. The following seems to work correctly for me:
async {
// I need the producer to run in it's own thread, not shared by the threadpool, because I rely on the consumer being allowed to block the thread with .Take
let! p = Async.StartChild producer
let maxConcurrent = 8
let! consumers =
List.init maxConcurrent (fun _ ->
Async.StartChild consumer)
|> Async.Parallel
do! p
for c in consumers do
do! c
}
|> Async.RunSynchronously

How can I create an F# async from a C# method with a callback?

Suppose I have some C# code that takes a callback:
void DoSomething(Action<string> callback);
Now, I want to use this in F#, but wrap it in an async. How would I go about this?
// Not real code
let doSomething = async {
let mutable result = null
new Action(fun x -> result <- x) |> Tasks.DoSomething
// Wait for result to be assigned
return result
}
For example, suppose DoSomething looks like this:
module Tasks
let DoSomething callback =
callback "Hello"
()
Then the output of the following should be "Hello":
let wrappedDoSomething = async {
// Call DoSomething somehow
}
[<EntryPoint>]
let main argv =
async {
let! resultOfDoSomething = wrappedDoSomething
Console.WriteLine resultOfDoSomething
return ()
} |> Async.RunSynchronously
0
The function Async.FromContinuations is, so to say, the "lowest level" of Async. All other async combinators can be expressed in terms of it.
It is the lowest level in the sense that it directly encodes the very nature of async computations - the knowledge of what to do in the three possible cases: (1) a successful completion of the previous computation step, (2) a crash of the previous computation step, and (3) cancellation from outside. These possible cases are expressed as the three function-typed arguments of the function that you pass to Async.FromContinuations. For example:
let returnFive =
Async.FromContinuations( fun (succ, err, cancl) ->
succ 5
)
async {
let! res = returnFive
printfn "%A" res // Prints "5"
}
|> Async.RunSynchronously
Here, my function fun (succ, err, cancl) -> succ 5 has decided that it has completed successfully, and calls the succ continuation to pass its computation result to the next step.
In your case, the function DoSomething expresses only one of the three cases - i.e. "what to do on successful completion". Once you're inside the callback, it means that whatever DoSomething was doing, has completed successfully. That's when you need to call the succ continuation:
let doSometingAsync =
Async.FromContinuations( fun (succ, err, cancl) ->
Tasks.DoSomething( fun res -> succ res )
)
Of course, you can avoid a nested lambda-expression fun res -> succ res by passing succ directly into DoSomething as callback. Unfortunately, you'll have to explicitly specify which type of Action to use for wrapping it, which negates the advantage:
let doSometingAsync =
Async.FromContinuations( fun (succ, err, cancl) ->
Tasks.DoSomething( System.Action<string> succ )
)
As an aside, note that this immediately uncovered a hole in the DoSomething's API: it ignores the error case. What happens if DoSomething fails to do whatever it was meant to do? There is no way you'd know about it, and the whole async workflow will just hang. Or, even worse: the process will exit immediately (depending on how the crash happens).
If you have any control over DoSomething, I suggest you address this issue.
You can try something like:
let doSomething callback = async {
Tasks.DoSomething(callback)
}
If your goal is to define the callback in the method you could do something like:
let doSomething () = async {
let callback = new Action<string>(fun result -> printfn "%A" result )
Tasks.DoSomething(callback)
}
If your goal is to have the result of the async method be used in the DoSomething callback you could do something like:
let doSomething =
Async.StartWithContinuations(
async {
return result
},
(fun result -> Tasks.DoSomething(result)),
(fun _ -> printfn "Deal with exception."),
(fun _ -> printfn "Deal with cancellation."))

F# ASync.Parallel plus main thread

I have an F# program to copy files that I want to work asynchronously. So far I have:
let asyncFileCopy (source, target, overwrite) =
let copyfn (source,target,overwrite) =
printfn "Copying %s to %s" source target
File.Copy(source, target, overwrite)
printfn "Copyied %s to %s" source target
let fn = new Func<string * string * bool, unit>(copyfn)
Async.FromBeginEnd((source, target, overwrite), fn.BeginInvoke, fn.EndInvoke)
[<EntryPoint>]
let main argv =
let copyfile1 = asyncFileCopy("file1", "file2", true)
let copyfile2 = asyncFileCopy("file3", "file4", true)
let asynctask =
[copyfile1; copyfile2]
|> Async.Parallel
printfn "doing other stuff"
Async.RunSynchronously asynctask |> ignore
which works (the files are copied) but not in the way I want. I want to start the parallel copy operations so that they begin copying. Meanwhile, I want to continue doing stuff on the main thread. Later, I want to wait for the asynchronous tasks to complete. What my code seems to do is set up the parallel copies, then do other stuff, but not actually execute the copies until it hits Async.Runsychronously.
Is there a way to, in effect, Async.Run"a"synchronously, to get the copies started in the thread pool, then do other stuff and later wait for the copies to finish?
Figured it out:
let asynctask =
[copyfile1; copyfile2]
|> Async.Parallel
|> Async.StartAsTask
let result = Async.AwaitIAsyncResult asynctask
printfn "doing other stuff"
Async.RunSynchronously result |> ignore
printfn "Done"
The key is using StartAsTask, AwaitIAsyncResult and only later RunSynchronously to await task completion

f# perform await async methods in list.iteri

I have the following code in F# 4.0
let processEscalation escalationAction (escalationEvents:UpdateCmd.Record list) =
printf "%A" Environment.NewLine
printf "Started %A" escalationAction
escalationEvents
|> List.iter ( fun x ->
printf "%A" Environment.NewLine
printf "escalation %A for with action: %A" x.incident_id escalationAction
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
|> Async.AwaitTask
|> ignore)
let ComposeEscalation() =
let escalationlevels = ["ESC1 REACHED"; "ESC2 REACHED"; "ESC3 REACHED"]
escalationlevels
|> List.map getEscalationEvents
|> List.iteri (fun i x -> processEscalation escalationlevels.[i] x)
where the following line is a call to a C# async method that that returns Task
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
The compose escalation method calls the processEscalation three times.
However, the second call starts before the first call is complete.
How can I make sure that the the last line, list.iteri awaits and processes them sequentially?
Perhaps the processEscalation should be in an async computation expression?
What Async.AwaitTask does is that it returns an Async computation that can be used to wait for the task to complete. In your case, you never do anything with it, so the loop just proceeds to the next iteration.
You want something like this:
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
|> Async.AwaitTask
|> Async.RunSynchronously
|> ignore
This should have the effect you expect, though certainly there are nicer, more composable ways of expressing such logic.
Edit: What I meant was something like this, a counterpart to the core Async.Parallel function:
module Async =
let sequential (asyncs: seq<Async<'t>>) : Async<'t []> =
let rec loop acc remaining =
async {
match remaining with
| [] -> return Array.ofList (List.rev acc)
| x::xs ->
let! res = x
return! loop (res::acc) xs
}
loop [] (List.ofSeq asyncs)
Then you can do something along these lines:
escalationEvents
// a collection of asyncs - note that the task won't start until the async is ran
|> List.map (fun x ->
async {
let task =
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
return! Async.AwaitTask task
})
// go from a collection of asyncs into an async of a collection
|> Async.sequential
// you don't care about the result, so ignore it
|> Async.Ignore
// now that you have your async, you need to run it in a way that makes sense
// in your context - Async.Start could be another option.
|> Async.RunSynchronously
The upside here is that instead of bundling everything into a single loop, you've split the computation into well-delimited stages. It's easy to follow and refactor (e.g. if you need to process those events in parallel instead, you just switch out one step in the pipeline).

Running Async computations with timeout

I don't understand the semantics of Async.RunSynchronously when given a timeout argument. Why doesn't the following example terminate?
let runInMaxTime (time: int) (work: Async<'T>) =
try
Async.RunSynchronously(work,time)
|> Some
with
| _ ->
None
let rec forever () : unit =
printfn "Forever looping"
forever ()
// Never terminates
let x = runInMaxTime 10 <| async {return forever ()}
What would a proper implementation of runInMaxTime look like? The only thing I've come up with that works is based on Tasks and cancellation tokens.
The timeout and cancellation token cannot arbitrarily stop an async computation, they can only do so when the computation yields, by using let!, do! etc. Your example computation doesn't do that.
This would work as you expect.
let rec forever () : Async<unit> =
async {
printfn "Forever looping"
return! forever()
}
let x = runInMaxTime 1000 <| forever()

Resources