Upload
tomas-petricek
View
1.032
Download
0
Embed Size (px)
DESCRIPTION
Talk about server-side programming in F# from Functional Programming eXchange 2011 at SkillsMatter. The talk progresses from callback-based programming to asynchronous workflows and to agents in F#.
Citation preview
F# on the Server-Side
Tomáš Petříček
http://tomasp.net/blog | @tomaspetricek
About me
Wrote book about F#Introduce functional conceptsand F# to C# developers
Worked with the F# teamContributed to VS 2010 integration
Reactive programming research
First blog post about F# in May 2006
Server-side specifics
Reliability and scalability is essential
Many concurrent requests
Many I/O bound operations
Exist to share state with clients
We need different programming style!
Plan of the talk
Event based Asynchronous workflows Agent based
Event-based style
Programming using explicit callbacks
Solves part of the problemInherently concurrent model
Avoids blocking of threads
Gaining popularity (e.g. Node.js)
HttpServer.Start("http://localhost:8080/", fun ctx -> WebClient.DownloadAsync(getProxyUrl(ctx), fun data -> ctx.ResponseStream.WriteAsync(data, fun res -> ctx.ResponseStream.Close())))
Limitations of event-based style
What we really wanted to write?
What we really should write…
let data = WebClient.DownloadAsync(getProxyUrl(ctx))ctx.ResponseStream.WriteAsync(data)ctx.ResponseStream.Close()
try let data = WebClient.DownloadAsync(getProxyUrl(ctx)) ctx.ResponseStream.WriteAsync(data)finally ctx.ResponseStream.Close()
Plan of the talk
Event based Asynchronous workflows Agent based
F# asynchronous workflows
Write usual sequential code
Asynchronous calls using let! and do!
Supports all F# control-flow constructs
let copyPageTo url outputStream = async { try let! html = WebClient.AsyncDownload(url) do! outputStream.AsyncWrite(html) finally ctx.ResponseStream.Close() }
F# asynchronous workflows
Translated using some primitive operations
Looks similar to event-based programs…
Many thanks to Haskell for Monads™!
let copyPageTo url outputStream = async.TryFinally( async.Bind(WebClient.AsyncDownload(url), fun html -> async.Bind( outputStream.AsyncWrite(html), async.Zero() )), fun () -> ctx.ResponseStream.Close())
Demo
HTTP proxy using asynchronous workflows
Plan of the talk
Event based Asynchronous workflows Agent based
Agent-based programming
Program consists of agents (processes)Lightweight and asynchronous
Agents communicate via messagesThread-safe and queued
Enables parallelismDifferent agents vs. multiple instances
Many thanks to Erlang for Agents™!
Simple agent in F#
Send Hello to the caller
Waiting for message is asynchronous
Can perform long-running I/O before replying
Calling agent asynchronously
let echo = Agent.Start(fun agent -> async { while true do let! name, rchan = agent.Receive() rchan.Reply("Hello " + name) })
let! str = echo.PostAndAsyncReply(fun ch -> "Tomas", ch)
Demo
Caching web pages using agent
Summary and Q&A
Event-based is good starting point
F# workflow syntax really helps
Asynchronous workflows enable agents
Use agents for server-side state