f#语言对异步程序设计的支持

24
Async Programming in F# Zhao Jie - SNDA - June 2010

Upload: jeffz

Post on 10-May-2015

2.738 views

Category:

Technology


24 download

DESCRIPTION

如今的Web应用、 Silverlight以及各种分布式系统让异步解决方案有了更进一步的需求。F#是微软.NET平台上的函数式编程语言,并添加了不少让并行及异步编程变得有趣且轻松的特性。本次演讲将讨论F#的核心概念,并探讨F#中的不可变性、函数式设计、异步工作流、代理等特性是如何应对真实应用中的异步挑战的。

TRANSCRIPT

Page 1: F#语言对异步程序设计的支持

Async Programming in F#

Zhao Jie - SNDA - June 2010

Page 2: F#语言对异步程序设计的支持

About Me

• 赵劼 / 老赵 / Jeffrey Zhao / 赵姐夫

• Programmer

• Blogger - http://blog.zhaojie.me/

• Twitterer: @jeffz_cn

• F#, Scala, JavaScript, Python, .NET, mono...

• Java (as the language) hater

Page 3: F#语言对异步程序设计的支持

What’s F#

• Languages by Don Syme, MS Research

• Strongly Statically Typed Language

• Functional Language with OO Ability

• General Purpose Language

Page 4: F#语言对异步程序设计的支持

Async programming is complex ...

Page 5: F#语言对异步程序设计的支持

...and will only getmore complex in the

near future

Page 6: F#语言对异步程序设计的支持

Four Big Concurrency Challenges

• Shared State

• Code Locality

• I/O Parallelism

• Scaling Up

Page 7: F#语言对异步程序设计的支持

Shared State

• Difficult to maintain and test

• Very difficult to parallelize

• Locking is fundamentally error prone‣ Must guess where parallelism is needed

‣ All consumers need to participate

Page 8: F#语言对异步程序设计的支持

Demo: Immutability

Page 9: F#语言对异步程序设计的支持

Immutability in F#

• Immutable Union

• Immutable Record

• Immutable Set

• Immutable Objects

• Immutable Tuple

• Immutable List

• Immutable Map

• Immutable ...

Page 10: F#语言对异步程序设计的支持

Code Locality

• Used to expressing algorithms linearly

• Async requires logical division of algorithms

• Very difficult to‣ Combine multiple asynchronous operations

‣ Deal with exceptions and cancellation

Page 11: F#语言对异步程序设计的支持

Asynchronous Workflow

async  {  ...  }

Page 12: F#语言对异步程序设计的支持

async  {        let!  res  =  <async  work>        ...  }

React

an HTTP Responsean UI Event

a Timer Callbacka Query Response

a Web Servcie Responsea Disk I/O Completion

an Agent Message

Page 13: F#语言对异步程序设计的支持

async  {        let!  img  =  AsyncRead  "http://..."        printfn  "loaded!"        do!  AsyncWrite  img  @"c:\..."        printfn  "saved!"  }

async.Delay(fun  -­‐>        async.Bind(AsyncRead  "http://...",  (fun  img  -­‐>                printfn  "loaded!"                async.Bind(AsyncWrite  img  @"c:\...",  (fun  ()  -­‐>                        printfn  "saved!"                        async.Return())))))

=

Page 14: F#语言对异步程序设计的支持

Demo: Code Locality

Page 15: F#语言对异步程序设计的支持

I/O Parallelism

• Software is often I/O-bound‣ Leveraging web services

‣ Working with data on disk

• Network and disk speeds increasing slower

• I/O resources are inherently parallel‣ Huge opportunity for performance

Page 16: F#语言对异步程序设计的支持

Demo: I/O Parallelism

Page 17: F#语言对异步程序设计的支持

Scaling to Multi-Machine

• To scale up, must to go beyond a single machine

• Multi-machine resources becoming common‣ Roll-you-own clusters with cheap hardware

‣ On-demand cloud compute with Azure

• But‣ Shared memory doesn’t scale

Page 18: F#语言对异步程序设计的支持

... the principle we go by is, don't expect to see a particular concurrency model put into C# because there're many different concurrency model ... it's more about finding things are common to to all kinds of concurrency ...

- Anders Hejlsberg

Page 19: F#语言对异步程序设计的支持

Demo: Agents

Page 20: F#语言对异步程序设计的支持

Concurrency Challenges

• Shared State - Immutability

• Code Locality - async { ... }

• I/O Parallelism - async { ... }

• Scaling to Multi-Machine - Agents

Page 21: F#语言对异步程序设计的支持

F#

• Modern, simple, powerful and productive

• Ready for production use with VS 2010

• Simplified parallel and async programming for today and tomorrow

• Support .NET 4.0 / 3.5 and mono

• Open Source *

Page 22: F#语言对异步程序设计的支持

F# Resourceshttp://fsharp.net

Programming F# Expert F# 2.0 Real World FP

Page 23: F#语言对异步程序设计的支持

Q & A

Page 24: F#语言对异步程序设计的支持

Thanks!