【问题标题】:R shiny future: plan(multiprocess)/plan(multicore) + Kill long running processR闪亮的未来:计划(多进程)/计划(多核)+杀死长时间运行的进程
【发布时间】:2019-04-08 15:18:26
【问题描述】:

我写这篇文章是为了在使用 plan(multiprocess) 或 plan(multicore) 并在我闪亮的应用程序中杀死长时间运行的进程方面寻求一些帮助。该应用程序具有多个未来事件(长时间运行的进程),这些事件在单击其相应的 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

由于此示例代表一个快速运行的进程,future() 命令在单击Cancel 之前执行。

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

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

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

【问题讨论】:

  • 什么是stopMulticoreFuture()
  • 请提供一个简单的可重现的例子。
  • stopMulticoreFuture() 是一个“向运行未来的进程发送终止和终止信号的函数,并且仅适用于在多核计划上运行的未来”。这是来自ipc 包。
  • stopMulticoreFuture() 的用例已记录在此处htmlpreview.github.io/?https://github.com/fellstat/ipc/blob/…
  • 仅供参考,这就是为什么提供完全可重复的示例如此有用的原因,包括加载了哪些包等,即包括 library() 调用和 sessionInfo()

标签: r asynchronous shinydashboard kill-process


【解决方案1】:

这里有几个问题:

  1. 您缺少library(promises)plan(multicore)library(ipc)
  2. fut 不是一个未来,它是一个承诺,因为 %...&gt;%,所以 stopMulticoreFuture 不会起作用。
  3. ObserveEvent 表达式需要返回承诺以外的内容,否则您的 UI 将阻塞。
  4. 由于stopMulticoreFuture 只是杀死进程,我不能保证它可以与创建子进程的system 调用一起工作。您可能需要找出这些的 pid 值并自己杀死它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2020-09-24
    • 1970-01-01
    • 2018-10-14
    相关资源
    最近更新 更多