How does F#’s async really work?

A few things.

First, the difference between

let resp = req.GetResponse()

and

let! resp = req.AsyncGetReponse()

is that for the probably hundreds of milliseconds (an eternity to the CPU) where the web request is ‘at sea’, the former is using one thread (blocked on I/O), whereas the latter is using zero threads. This is the most common ‘win’ for async: you can write non-blocking I/O that doesn’t waste any threads waiting for hard disks to spin around or network requests to return. (Unlike most other languages, you aren’t forced to do inversion of control and factor things into callbacks.)

Second, Async.StartImmediate will start an async on the current thread. A typical use is with a GUI, you have some GUI app that wants to e.g. update the UI (e.g. to say “loading…” somewhere), and then do some background work (load something off disk or whatever), and then return to the foreground UI thread to update the UI when completed (“done!”). StartImmediate enables an async to update the UI at the start of the operation and to capture the SynchronizationContext so that at the end of the operation is can return to the GUI to do a final update of the UI.

Next, Async.RunSynchronously is rarely used (one thesis is that you call it at most once in any app). In the limit, if you wrote your entire program async, then in the “main” method you would call RunSynchronously to run the program and wait for the result (e.g. to print out the result in a console app). This does block a thread, so it is typically only useful at the very ‘top’ of the async portion of your program, on the boundary back with synch stuff. (The more advanced user may prefer StartWithContinuationsRunSynchronously is kinda the “easy hack” to get from async back to sync.)

Finally, Async.Parallel does fork-join parallelism. You could write a similar function that just takes functions rather than asyncs (like stuff in the TPL), but the typical sweet spot in F# is parallel I/O-bound computations, which are already async objects, so this is the most commonly useful signature. (For CPU-bound parallelism, you could use asyncs, but you could also use TPL just as well.)

Leave a Comment