反应堆与Coroutines:GlobalsCope.Launch vs Mono.Subscribeon(调度程序。)。subscribe()
在带有 netty 服务器的 spring-boot 反应式应用程序中使用 kotlin。我有一项应该启动异步操作的服务。此操作不应阻止原始请求处理(即,请求处理应完成,而操作可能会在其自己的生命周期中继续运行)。
从 Spring 响应式的角度来看,我可以使用 Reactor 来做到这一点:
Mono.fromRunnable<Unit> {
runBlocking {
// do other operation
}
}
.subscribeOn(Schedulers.boundedElastic())
.subscribe()
但我也可以使用 kotlin 协程来做类似的事情:
GlobalScope.launch {
withContext(Dispatchers.IO) {
// do other operation
}
}
两者都有效地在不同的线程上安排操作并满足启动异步操作的要求。但这里的权衡是什么?是否有任何争论可以解释为什么其中一个“更好”?
我最初的想法是“反应堆方法”创建新线程,而协程方法可能性能更高?
Using kotlin in a spring-boot reactive application with netty server. I have a service that should launch an async operation. This operation should not block the original request processing (i.e. the reqeust handling should finish while the operation might keep running in its own life-cycle).
Coming from the Spring reactive perspective I can do this using Reactor:
Mono.fromRunnable<Unit> {
runBlocking {
// do other operation
}
}
.subscribeOn(Schedulers.boundedElastic())
.subscribe()
but I also could do something like this, using kotlin coroutines:
GlobalScope.launch {
withContext(Dispatchers.IO) {
// do other operation
}
}
Both effectively schedule the operation on a different thread and fulfill the requirement of launching that async operation. But what is the trade-off here? Is there any arguments why one of them is "better"?
My initial thought is that the "Reactor-approach" creates new threads while the coroutine approach might be more performant?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论