R闪亮的未来:计划(多进程)/计划(多核)+杀死长时间运行的进程

cha*_*has 6 asynchronous r kill-process shinydashboard

我写这篇文章是为了寻求一些帮助,使用计划(多进程)或计划(多核)并在我闪亮的应用程序中杀死长时间运行的进程.该应用程序具有多个未来事件(长时间运行的进程),这些事件在单击其相应的actionButton时运行.下面是app中服务器函数中使用的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)
Run Code Online (Sandbox Code Playgroud)

当我单击"取消"按钮时,UI被禁用但控制台显示以下警告,并且命令仍然在控制台中执行.

Warning: Error in stopMulticoreFuture: stopMulticoreFuture only works on multicore futures
Run Code Online (Sandbox Code Playgroud)

由于此示例表示快速运行的进程,因此在单击之前将执行future()命令Cancel.

在实际情况下,即使在单击"取消"之后,未来内部的命令(长进程)仍然在警告之后在控制台中运行,同时UI已被禁用.

该应用程序目前在具有4个核心的MAC上运行.我怎么能杀死在控制台中运行的进程而不只是禁用UI?

我目前正在测试我的应用程序,并且非常乐意在规划多进程/多核程序时使用专家输入并终止进程以使应用程序在并行用户之间运行异步进程时高效.最终的应用程序将在具有4个虚拟CPU的Ubuntu机器上运行.

Ian*_*ows 1

这里有几个问题:

  1. 你失踪了library(promises)plan(multicore)并且library(ipc)
  2. fut不是未来,它是一个承诺,因为%...>%,所以stopMulticoreFuture不会实现它。
  3. ObserveEvent表达式需要返回除 Promise 之外的其他内容,否则您的 UI 将阻塞。
  4. 由于stopMulticoreFuture只是终止进程,我不能向您保证它将与system创建子进程的调用一起使用。您可能需要找出这些的 pid 值并自己杀死它们。