首页 > 解决方案 > 为什么这个承诺会导致 R 中的阻塞?

问题描述

在下面,我不明白为什么代码块。我希望在打印“Sys.sleep 之后”之前立即打印“繁荣”。我认为 future_promise 表达式中的所有内容都不应导致阻塞。我有什么误解?

library(promises)

test <- function() {
  promise <- future_promise({
    Sys.sleep(10)
    print("after Sys.sleep")
  })
  print("boom")
}

test()

好的,这就是我所追求的!:

library(promises)
plan(multiprocess)

test2 <- function() {
  future({
    # expensive operations go here...
    Sys.sleep(10)
  }) %...>% (function(result) {
    print("after Sys.sleep")
  })
  print("boom")

}

test2()

标签: r

解决方案


这符合我的预期:

library(promises)
plan(multiprocess)

test2 <- function() {
  future({
    Sys.sleep(10)
  }) %...>% (function(result) {
    print("after Sys.sleep")
  })
  print("boom")

}

test2()

推荐阅读