You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中future适配为future_promise的PID及取值问题

我尝试将R中单独使用future的代码改造为future_promise(部分原因出于好奇),但遇到了一些问题:

  • 无法像之前通过quux$job$pid获取子进程的PID,用于父/当前进程追踪;
  • 已解决:通过%>%替代%...%实现then操作在父进程执行;
  • 无法获取future_promise的返回值,调用value()函数报错。

单独使用future的示例代码

library(future)
# library(logger)
plan(multicore, workers = 10)
(ppid <- Sys.getpid())
# [1] 2704731
quux <- future({
  logger::log_info(msg="in", ppid=ppid)
  Sys.sleep(3)
  out <- mtcars[sample(32, size=3),]
  logger::log_info(msg="out", ppid=ppid); out; }, seed=TRUE)
# {"time":"2022-11-12 14:13:43","level":"INFO","pid":3189217,"user":"r2","msg":"in","ppid":2704731}
quux$job$pid
# [1] 3189217
1+3   ### demo that the repl is still interactive
# [1] 4
# {"time":"2022-11-12 14:13:46","level":"INFO","pid":3189217,"user":"r2","msg":"out","ppid":2704731}
value(quux)
#                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
# Merc 240D      24.4   4 146.7  62 3.69 3.190 20.0  1  0    4    2
# Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.9  1  1    4    1
# Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2

使用future_promise的示例代码

library(promises)
did_something <- FALSE
quux <- future_promise({
  logger::log_info(msg="in", ppid=ppid)
  Sys.sleep(3)
  out <- mtcars[sample(32, size=3),]
  out
}, seed=TRUE) %>%
  then(~ {
    logger::log_info(msg="out", ppid=ppid)
    did_something <<- TRUE
    .
  })
# >
# {"time":"2022-11-12 14:40:53","level":"INFO","pid":2704731,"user":"r2","msg":"out","ppid":2704731}
# >
quux$job$pid
# NULL
did_something
# [1] FALSE
quux
# <Promise [pending]>
# >
# {"time":"2022-11-12 14:40:53","level":"INFO","pid":2704731,"user":"r2","msg":"out","ppid":2704731}
quux
# <Promise [fulfilled: data.frame]>
did_something
# [1] TRUE
value(quux)
#  Error in UseMethod("value") :
#    no applicable method for 'value' applied to an object of class "promise"

请问这是future_promise结合then子句的标准用法吗?显然我对future+promises的基础概念存在误解。
注:此代码并非用于Shiny环境。

会话信息

sessionInfo()
# R version 4.1.3 (2022-03-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 22.04.1 LTS
# Matrix products: default
# BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
# LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
# locale:
#  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8
#  [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
#  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C
# [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base
# other attached packages:
# [1] promises_1.2.0.1  future_1.29.0     data.table_1.14.2 dplyr_1.0.10      optparse_1.7.3
# loaded via a namespace (and not attached):
#  [1] Rcpp_1.0.9        later_1.3.0       pillar_1.8.1      compiler_4.1.3    tools_4.1.3
#  [6] odbc_1.3.3        digest_0.6.29     bit_4.0.4         jsonlite_1.8.0    lifecycle_1.0.1
# [11] tibble_3.1.8      pkgconfig_2.0.3   rlang_1.0.5       bench_1.1.2       DBI_1.1.3
# [16] cli_3.3.0         parallel_4.1.3    curl_4.3.2        yaml_2.3.5        fastmap_1.1.0
# [21] globals_0.16.1    generics_0.1.3    vctrs_0.4.1       fs_1.5.2          hms_1.1.2
# [26] RPushbullet_0.3.4 bit64_4.0.5       getopt_1.20.3     tidyselect_1.1.2  glue_1.6.2
# [31] listenv_0.8.0     R6_2.5.1          redux_1.1.3       processx_3.7.0    parallelly_1.32.1
# [36] fansi_1.0.3       profmem_0.6.0     callr_3.7.2       purrr_0.3.4       logger_0.2.2
# [41] blob_1.2.3        magrittr_2.0.3    codetools_0.2-18  ps_1.7.1          ellipsis_0.3.2
# [46] assertthat_0.2.1  config_0.3.1      arrow_10.0.0      utf8_1.2.2        crayon_1.5.1

内容的提问来源于stack exchange,提问作者r2evans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 11:15:35