R Shiny 的 future : plan(multiprocess)/plan(multicore) + Kill long running process

标签 r asynchronous shinydashboard kill-process

我写这篇文章是为了在我 Shiny 的应用程序中使用计划(多进程)或计划(多核)并杀死长时间运行的进程寻求一些帮助。该应用程序有多个 future 事件(长时间运行的进程),这些事件在单击其相应的 actionButton 时运行。下面是在应用程序的服务器功能中使用的 future() 命令的示例应用程序。我一直在使用 stopMulticoreFuture(fut) 来终止进程。

library(shiny)
library(shinydashboard)
library(promises)
plan(multicore)
library(ipc)
sidebar <- dashboardSidebar(width = 200, sidebarMenu(id = "tabs",
                                                     menuItem("File", tabName = "tab1", icon = icon("fas fa-file"))))
body <- tabItem(tabName = "tab1",h2("Input File"),
                fluidRow(tabPanel(
                    "Upload file",
                    value = "upload_file",
                    fileInput(
                      inputId = "uploadFile",
                      label = "Upload Input file",
                      multiple = FALSE,
                      accept = c(".txt")
                    ),
                    checkboxInput('header', label = 'Header', TRUE)
                  ),
                  box(
                    title = "Filter X rows",
                    width = 7,
                    status = "info",
                    tabsetPanel(
                      id = "input_tab",
                      tabPanel(
                        "Parameters",
                        numericInput(
                          "nrows",
                          label = "Entire number of rows",
                          value = 5,
                          max = 10
                        ),
                        actionButton("run", "Analyze"),
                        actionButton("cancel", "Cancel")
                      ),
                      tabPanel(
                        "Results",
                        value = "results",
                        navbarPage(NULL,
                                   tabPanel(
                                     "Table", DT::dataTableOutput("res_table"), 
                                     icon = icon("table")
                                   )),
                        downloadButton("downList", "Download")
                      )
                    )
                  )
                ))
ui <-
  shinyUI(dashboardPage(
    dashboardHeader(title = "TestApp", titleWidth = 150),
    sidebar,dashboardBody(tabItems(body))
  ))


server <- function(input, output, session) {
  file_rows <- reactiveVal()
  observeEvent(input$run, {
    prog <- Progress$new(session)
    prog$set(message = "Analysis in progress",
             detail = "This may take a while...",
             value = NULL)
    file_nrows <- reactive({
      return(input$nrows)
    })

    file_nrows_value <- file_nrows()

    file_input <- reactive({
      return(input$uploadFile$datapath)
    })

    file_input_value <- file_input()

    fut<- NULL

    fut<<- future({system(paste(
      "cat",
      file_input_value,
      "|",
      paste0("head -", file_nrows_value) ,
      ">",
      "out.txt"
    ))
    head_rows <- read.delim("out.txt")
    head_rows
    }) %...>%
     file_rows() %>%
     finally(~prog$close())
})

  observeEvent(file_rows(), {
    updateTabsetPanel(session, "input_tab", "results")
    output$res_table <-
      DT::renderDataTable(DT::datatable(
        file_rows(),
        options = list(
          searching = TRUE,
          pageLength = 10,
          rownames(NULL),
          scrollX = T
        )
      ))
  })

  output$downList <- downloadHandler(
    filename = function() {
      paste0("output", ".txt")
    }, content = function(file) {
      write.table(file_rows(), file, row.names = FALSE)
    }
  )

  observeEvent(input$cancel,{
    stopMulticoreFuture(fut)
  })

}

shinyApp(ui = ui, server = server)

当我单击“取消”按钮时,UI 被禁用,但控制台显示以下警告,并且命令仍会在控制台中执行。
Warning: Error in stopMulticoreFuture: stopMulticoreFuture only works on multicore futures

由于这个例子代表了一个快速运行的过程,所以在单击 Cancel 之前会执行 future() 命令。 .

在实际情况下,即使在单击“取消”之后,将来(长过程)中的命令在警告后仍会在控制台中运行,而 UI 已被禁用。

该应用程序目前在具有 4 个内核的 MAC 上运行。我怎么能终止在控制台中运行的进程,而只是禁用 UI?

我目前正在测试我的应用程序,如果能够在规划多进程/多核和终止进程方面获得专家意见,使应用程序能够高效地在并行用户之间运行异步进程,那就太好了。最终的应用程序将在具有 4 个虚拟 CPU 的 Ubuntu 机器上运行。

最佳答案

这里有几个问题:

  • 您失踪了 library(promises) , plan(multicore)library(ipc) .
  • fut不是 future ,是因为%...>%的 promise , 所以 stopMulticoreFuture不会工作。
  • ObserveEvent表达式需要返回 promise 以外的东西,否则你的 UI 会阻塞。
  • stopMulticoreFuture只是杀死进程,我不能向您保证它可以与 system 一起使用创建子进程的调用。您可能需要找出这些的 pid 值并自己杀死它们。
  • 关于R Shiny 的 future : plan(multiprocess)/plan(multicore) + Kill long running process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53151431/

    相关文章:

    R 中 geom_abline() 的奇怪行为

    r - dplyr::summarize 按字母顺序排列,但我需要原始顺序

    node.js - 我可以在不深入嵌套代码的情况下执行许多异步数据库请求吗?

    node.js - nodejs异步的while

    r Shiny -uiOutput未在menuItem内呈现

    r - 直接将字符对象转换为数据框而不保存

    css - 使用 Foundation 4 CSS 时 R Markdown 绘制图像质量损失

    iphone - 当 dispatch_async 任务完成时如何通知我?

    shiny - 使用操作按钮启用和禁用侧边栏切换按钮

    r - 如何在 Shiny 中渲染循环内框的 html 内容?