【问题标题】:How can I shut down Rserve gracefully?如何优雅地关闭 Rserve?
【发布时间】:2014-12-01 05:37:24
【问题描述】:

我在 Mac 和 Ubuntu 中都尝试了很多选项。 我阅读了 Rserve 文档

http://rforge.net/Rserve/doc.html

对于 Rserve 和 RSclient 软件包:

http://cran.r-project.org/web/packages/RSclient/RSclient.pdf

http://cran.r-project.org/web/packages/Rserve/Rserve.pdf

我无法弄清楚在 Rserve 中打开/关闭连接以及“优雅地”关闭 Rserve 的正确工作流程是什么。

例如,在 Ubuntu 中,我使用 ./config --enable-R-shlib 从源代码安装 R(遵循 Rserve 文档),还在 /etc/Rserve.conf 中添加了“控制启用”行。

在 Ubuntu 终端中:

library(Rserve)
library(RSclient)
Rserve()
c<-RS.connect()
c ## this is an Rserve QAP1 connection

## Trying to shutdown the server
RSshutdown(c) 
Error in writeBin(as.integer....): invalid connection

RS.server.shutdown(c)
Error in RS.server.shutdown(c): command failed with satus code 0x4e: no control line present   (control commands disabled or server shutdown)

但是,我可以关闭连接:

RS.close(c)
>NULL
c ## Closed Rserve connection

关闭连接后,我也尝试了选项(也尝试了参数'c',即使连接已关闭):

RS.server.shutdown()
RSshutdown()

所以,我的问题是:

1- 如何优雅地关闭 Rserve?

2- 可以在没有 RSclient 的情况下使用 Rserve 吗?

我也看了

How to Shutdown Rserve(), running in DEBUG

但问题是指调试模式,也没有解决。 (我没有足够的声誉来评论/询问关闭是否在非调试模式下工作)。

还看了:

how to connect to Rserve with an R client

非常感谢!

【问题讨论】:

    标签: r rserve


    【解决方案1】:

    如果您无法在 R 中将其关闭,请运行以下代码以在终端中将其杀死。这些代码适用于 Mac。

    $ ps ax | grep Rserve  # get active Rserve sessions
    

    您将看到如下所示的输出。 29155 是活动 Rserve 会话的作业 ID。

    29155  /Users/userid/Library/R/3.5/library/Rserve/libs/Rserve
    
    38562  0:00.00 grep Rserve
    

    然后运行

    $ kill 29155 
    

    【讨论】:

      【解决方案2】:

      在Windows系统上,如果要关闭RServe实例,可以使用R中的system函数将其关闭。 比如R:

      library(Rserve)
      Rserve() # run without any arguments or ports specified
      system('tasklist /FI "IMAGENAME eq Rserve.exe"') # run this to see RServe instances and their PIDs
      system('TASKKILL /PID {yourPID} /F') # run this to kill off the RServe instance with your selected PID
      

      如果您已正确关闭带有该 PID 的 RServe 实例,则会出现以下消息:

      SUCCESS: PID xxxx 的进程已经终止。

      您可以通过输入检查RServe实例是否已关闭

      system('tasklist /FI "IMAGENAME eq Rserve.exe"')

      再次。如果不再有 RServe 实例在运行,您将收到消息

      信息:没有符合指定条件的任务正在运行。

      有关此主题的更多帮助和信息可以在this related question 中查看。

      请注意,较早的答案中提到的“RSClient”方法比这个更简洁、更容易,但我还是为那些开始RServe而不知道如何停止它的人提出了它。

      【讨论】:

        【解决方案3】:

        加载 Rserve 和 RSclient 包,然后连接到实例。

        > library(Rserve)
        > library(RSclient)
        
        > Rserve(port = 6311, debug = FALSE)
        > Rserve(port = 6312, debug = TRUE)
        
        Starting Rserve...
         "C:\..\Rserve.exe" --RS-port 6311
        Starting Rserve...
         "C:\..\Rserve_d.exe" --RS-port 6312 
        
        > rsc <- RSconnect(port = 6311)
        > rscd <- RSconnect(port = 6312)
        

        看起来他们正在运行...

        > system('tasklist /FI "IMAGENAME eq Rserve.exe"')
        > system('tasklist /FI "IMAGENAME eq Rserve_d.exe"')
        
        Image Name                     PID Session Name        Session#    Mem Usage
        ========================= ======== ================ =========== ============
        Rserve.exe                    8600 Console                    1     39,312 K
        Rserve_d.exe                 12652 Console                    1     39,324 K
        

        让我们关闭它们。

        > RSshutdown(rsc)
        > RSshutdown(rscd)
        

        他们走了……

        > system('tasklist /FI "IMAGENAME eq Rserve.exe"')
        > system('tasklist /FI "IMAGENAME eq Rserve_d.exe"')
        
        INFO: No tasks are running which match the specified criteria.
        

        Rserve 可以在没有 RSclient 的情况下使用,方法是使用 args 和/或配置脚本启动它。然后,您可以从其他程序(如 Tableau)或使用您自己的代码连接到它。 RSclient 提供了一种从 R 实例向 Rserve 传递命令/数据的方法。

        希望这会有所帮助:)

        【讨论】:

        • 谢谢!所以,如果我理解正确,为了将 R 进程与 Rserve 实例连接起来,我必须使用 RSclient,对吗?我还需要在 Rserve 中指定“args”命令(否则会出现致命错误),当使用 debug=TRUE 时,命令行会一直等待并以“错误:无法与 R 会话建立连接”结束。因此,使用 mac 时,似乎只有 debug=F 选项有效,而我使用了 system('ps aux | grep Rserve') [这会打开 2 个连接,具有两个不同的 ID..]。非常感谢!!
        • 这适用于 Rserve(port = 6311, debug = FALSE,args="--no-save")。谢谢!