I\'m trying to implement a pattern I read from Don Syme\'s blog
(https://blogs.msdn.microsoft.com/dsyme/2010/01/09/async-and-parallel-design-patterns-in-f-parallelizing-
F# asynchronous workflows allow you to implement asynchronous computations, however, F# makes a distinction between usual computation and asynchronous computations. This difference is tracked by the type-system. For example a method that downloads web page and is synchronous has a type string -> string
(taking URL and returning HTML), but a method that does the same thing asynchronously has a type string -> Async
. In the async
block, you can use let!
to call asynchronous operations, but all other (standard synchronous) methods have to be called using let
. Now, the problem with your example is that the GetData
operation is ordinary synchronous method, so you cannot invoke it with let!
.
In the typical F# scenario, if you want to make the GetData
member asynchronous, you'll need to implement it using an asynchronous workflow, so you'll also need to wrap it in the async
block. At some point, you will reach a location where you really need to run some primitive operation asynchronously (for example, downloading data from a web site). F# provides several primitive asynchronous operations that you can call from async
block using let!
such as AsyncGetResponse
(which is an asynchronous version of GetResponse
method). So, in your GetData
method, you'll for example write something like this:
let GetData (url:string) = async {
let req = WebRequest.Create(url)
let! rsp = req.AsyncGetResponse()
use stream = rsp.GetResponseStream()
use reader = new System.IO.StreamReader(stream)
let html = reader.AsyncReadToEnd()
return CalculateResult(html) }
The summary is that you need to identify some primitive asynchronous operations (such as waiting for the web server or for the file system), use primitive asynchronous operations at that point and wrap all the code that uses these operations in async
blocks. If there are no primitive operations that could be run asynchronously, then your code is CPU-bound and you can just use Parallel.map
.
I hope this helps you understand how F# asynchronous workflows work. For more information, you can for example take a look at Don Syme's blog post, series about asynchronous programming by Robert Pickering, or my F# web cast.