Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does f# async check its CancellationToken?

I'm reading F# for fun and profit - Asynchronous programming. Under Cancelling workflows they have the following example:

let testLoop = async {
    for i in [1..100] do
    // do something
    printf "%i before.." i
    
    // sleep a bit 
    do! Async.Sleep 10  
    printfn "..after"
    }

open System
open System.Threading

// create a cancellation source
let cancellationSource = new CancellationTokenSource()

// start the task, but this time pass in a cancellation token
Async.Start (testLoop,cancellationSource.Token)

// wait a bit
Thread.Sleep(200)  

// cancel after 200ms
cancellationSource.Cancel()

About this they say:

In F#, any nested async call will check the cancellation token automatically!

In this case it was the line:

do! Async.Sleep(10) 

As you can see from the output, this line is where the cancellation happened.

However, for me (VS2010, F# 2.0, F# Interactive) I get the following output. Notice how it also prints ..after after I have canceled the token. Are they simply wrong?

1 before....after
2 before....after
3 before....after
4 before....after
5 before....after
6 before....after
7 before....after
8 before....after
9 before....after
10 before....after
11 before....after
12 before....after
13 before..
val cancellationSource : CancellationTokenSource

>
..after

So perhaps the check for cancellation is done when entering Async.Sleep? No, then it would've printed:

13 before....after
14 before..
val cancellationSource : CancellationTokenSource

>

So it seems like the check is actually in the for-loop! I.e. it keeps running until the for loop after being cancelled. Is this how it works? What then if I'd rather want it to check after the sleep?

This question seems to hint at cancellation working like I described above: Can I explicitly check for cancellation / terminate async computation?

Edit: Regarding if this is only in FSI 2.0: What happens with the following loop, if one sleep 200ms, 2500ms and 4000ms respectively? Does it print middle?

let testLoop = async {
    for i in [1..5] do
    printf "%i before.." i
    do! Async.Sleep 2000
    printfn "..middle.."
    do! Async.Sleep 1000  
    printfn "..after"
    }
like image 671
johv Avatar asked Jan 17 '14 14:01

johv


People also ask

What does f say about f and f?

f, f′ and f″ Since (f′)′=f″, when f′ is increasing, f″ is positive. Similarly, when the slopes of tangent lines are decreasing, i.e. when f′ is decreasing, the function is concave down, as you can see in the second two graphs below. Since (f′)′=f″, when f′ is decreasing, f″ is negative.

What does it mean when f is negative?

Likewise, if x is a critical point of f(x) and the second derivative of f(x) is negative, then the slope of the graph of the function is zero at that point, but the curve of the graph is concave down. The only way to draw a graph like this to make the point x a local maximum of the function.

How does f relate to f?

Relationship between f, f' and f'' To our common sense, when f is always greater than o, then the function is always above x-axis, and when f is always less than 0, f is always below the x-axis. And if f is just greater than 0 at certain range, then it is just above x-axis at that corresponding range, vise versa.

When f is increasing f prime is?

The portion of the graph that moves upward in quadrant 1 is where f prime is positive and f is increasing. When we justify the properties of a function based on its derivative, we are using calculus-based reasoning. These are two valid justifications for why a function f is an increasing function: A.


1 Answers

I see the same results as you with F# 2.0 only in interactive Fsi. If I put the same code in a file and run fsi cancel.fsx, then the output does not have the final after and is what you expect.

Fsi v11 and v12 show the expected output for both ways of running the code.

This suggests there is some bug or difference when running Fsi v2.0 interactively that was fixed in a later version of FSharp.

like image 116
Leaf Garland Avatar answered Sep 20 '22 01:09

Leaf Garland