Created
January 24, 2019 19:01
-
-
Save sgoguen/e278a84e2187f6d83031a38318cfed95 to your computer and use it in GitHub Desktop.
How to cancel an agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#quit | |
#load "LoadServer.fsx" | |
open System | |
open System.Threading | |
open LoadServer.Watcher | |
// Agents can be cancelled if you want to stop a runaway | |
// process | |
type Agent<'a> = MailboxProcessor<'a> | |
// This start function will create an agent and provide it | |
// with a cancellation token so we can stop it. | |
let startAgent(f) = | |
let cts = new CancellationTokenSource() | |
let agent = new Agent<_>(f, cts.Token) | |
agent.Start() | |
// We return the agent and the token | |
(agent, cts) | |
// Now lets create the agent and capture the token | |
let (agent1, cancelToken) = startAgent<int> <| fun inbox -> | |
async { | |
for x in 1..1000 do | |
do! Async.Sleep(1000) | |
print(("Agent", x)) |> ignore | |
} | |
// Let's call the cancel method | |
cancelToken.Cancel() | |
// That's it. Do you have any questions about Agents | |
// you'd like to see demoed? Just mention me. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. Create a project and add Steego.FsPad as a Nuget dependency | |
// 2. Build your project and set this folder to your output | |
#I "bin/Debug/net461/" | |
#r "FSharp.Core.dll" | |
#r "Suave.dll" | |
#r "Steego.FsPad.dll" | |
module Watcher = | |
// Start the web server (websockets) | |
let web = FsPad.Web(3000) | |
// Define a hacky printer function | |
let print(x) = web.Dump(x, 5); web.Dump(x, 5); sprintf "%A" x | |
// Register it with fsi | |
do fsi.AddPrinter(print) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment