Search Wiki:
Content from PDC2009 talk on F# for Parallel and Asynchronous Programming

Demo Code and Slides:
Demo Code and Slides v1.0

Resources:
- Video of the PDC 2009 talk is available at http://microsoftpdc.com/Sessions/FT20.
- I've blogged about some additional topics related to this at http://blogs.msdn.com.
- Get F# at http://fsharp.net.
- Get the Windows Azure SDK at http://www.microsoft.com/downloads/details.aspx?FamilyID=772990da-8926-4db0-958f-95c1da572c84


Note: You'll need a Windows Azure Storage account (get an account token at http://go.microsoft.com/fwlink/?LinkID=129453) and will need to upload some image files into a 'photogallery' folder in your Azure blob storage to use the async and agent parts of the sample code. Then search/replace all occurenes of <YOUR_ACCOUNT_NAME_HERE> with your account name, and <YOUR_ACCOUNT_KEY_HERE> with your azure storage account key.

One of the samples for easy reference:

namespace FSharpAsync3
 
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.StorageClient
open System.IO
 
// Parallel and async image downloader
// Leverages CPU and I/O parallelism
// Potentially has multiple pending network requests, multiple pending disk writes and multiple cores busy concurrently
// Async objects support functional compositional programming model
 
type AzureImageDownloader(folder) = 
    let acct = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<YOUR_ACCOUNT_NAME_HERE>;AccountKey=<YOUR_ACCOUNT_KEY_HERE>")
 
    let storage = acct.CreateCloudBlobClient();
    let container = storage.GetContainerReference(folder);
    let _ = container.CreateIfNotExist()
 
    let downloadImageAsync(blob : CloudBlob) =
      async {
        let! pixels = blob.AsyncDownloadByteArray()
        let fileName = "thumbs-" + blob.Uri.Segments.[blob.Uri.Segments.Length-1]
        use outStream =  File.OpenWrite(fileName)
        do! outStream.AsyncWrite(pixels, 0, pixels.Length)
        return fileName
      }
 
    member this.DownloadAll() = 
      container.ListBlobs()
      |> Seq.map (fun blob ->
        downloadImageAsync(container.GetBlobReference(blob.Uri.ToString())))
      |> Async.Parallel
      |> Async.StartAsTask

Last edited Dec 9 2009 at 10:25 PM  by LukeH, version 13
Updating...
Page view tracker