Skip to content

Instantly share code, notes, and snippets.

@grishace
Created January 25, 2020 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grishace/13b786dff6bee5c852206e7c5c5cbb2b to your computer and use it in GitHub Desktop.
Save grishace/13b786dff6bee5c852206e7c5c5cbb2b to your computer and use it in GitHub Desktop.
Trying System.Threading.Channels
open System
open System.Threading
open System.Threading.Channels
open FSharp.Control
let channel =
Channel.CreateUnbounded<int>(UnboundedChannelOptions(SingleWriter=true, SingleReader=true))
let mutable cnt = 0
let timerEvent _ =
cnt <- cnt + 1
channel.Writer.WriteAsync(cnt).AsTask() |> Async.AwaitTask |> Async.Start
#nowarn "40"
let rec asq =
asyncSeq {
yield channel.Reader.ReadAsync().AsTask() |> Async.AwaitTask
yield! asq
}
[<EntryPoint>]
let main _ =
let timer = new Timer(TimerCallback(timerEvent))
timer.Change(0, 1000) |> ignore
asq
|> AsyncSeq.mapAsync id
|> AsyncSeq.iter (fun i -> Console.WriteLine(sprintf "%d" i))
|> Async.Start
Console.ReadLine() |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment