f# - Giraffe F# 中的流式序列
问题描述
我想知道在 Giraffe 中流式传输 F# seq 的最简单方法是什么。不多,但这是我所拥有的:
module HttpHandler =
let handlerGuids : HttpHandler =
handleContext(
fun ctx ->
task {
let collection =
seq {
let mutable i = 0
while (not ctx.RequestAborted.IsCancellationRequested) && i <10 do
i <- i + 1
Async.Sleep(2000) |> Async.RunSynchronously
yield Guid.NewGuid()
}
return! ctx.WriteJsonChunkedAsync collection
})
let router: HttpFunc -> HttpContext -> HttpFuncResult =
choose [ route "/" >=> handlerGuids ]
我在 C# 中也有这个测试
[Fact]
public async void Test1()
{
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);
var requestUri = "http://localhost:8080/";
var stream = await httpClient.GetStreamAsync(requestUri);
using var reader = new StreamReader(stream);
while (!reader.EndOfStream) {
var currentLine = reader.ReadLine();
}
}
但它会等到服务器上生成所有 guid。有人可以给我一些提示吗?Giraffe 文档中提到了流媒体,但它与文件有关。
解决方案
推荐阅读
- php - Coinbase Pro - IP 与 IP 白名单不匹配问题,但 IP 已列入白名单
- flutter - 堆栈内的 IntrinsicHeight
- javascript - 为什么我的方法在点击时在 Framework7 中被调用两次?
- if-statement - 如何将替代值分配给相对于 Ox 中的另一个变量的变量(有点类似于 C++)
- jenkins - 包含所有参数值列表或参数列表中的单个值的 Jenkinsfile
- android - Google Play 游戏服务 - 如何切换到新的 Firebase 项目?
- javascript - 如何检查表 td 是否至少有一个值?
- botframework - 在 Bot Composer 中连接现有的 QnA 知识库
- matlab - 从matlab中的结构创建表 - 对齐
- c++ - 通常的算术转换如何工作?