Created
January 25, 2020 20:33
-
-
Save grishace/13b786dff6bee5c852206e7c5c5cbb2b to your computer and use it in GitHub Desktop.
Trying System.Threading.Channels
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
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