如何优雅地关闭Rserve?

15

我在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(遵循Rserve文档)从源代码安装了R,并在/etc/Rserve.conf中添加了“control enable”行。

在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?

我也看过

如何关闭运行在DEBUG模式下的Rserve()

但是这个问题涉及调试模式,并且也没有解决。(我没有足够的声望来评论/询问关闭是否在非调试模式下工作。)

还看了一下:

如何使用R客户端连接到Rserve

非常感谢!

3个回答

17

加载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的情况下使用它。然后,您可以从其他程序(如Tableau)或自己的代码连接到它。RSclient提供了一种从R实例传递命令/数据到Rserve的方式。

希望这可以帮助您 :)


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

6
在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已终止。
您可以通过输入
system('tasklist /FI "IMAGENAME eq Rserve.exe"')
再次检查是否关闭了RServe实例。如果不再运行任何RServe实例,则会收到消息
INFO: 没有符合指定条件的任务正在运行。
有关此主题的更多帮助和信息,请参见this related question
请注意,早期答案中提到的“RSClient”方法比这种方法更整洁、更容易,但我仍然提出该方法供那些不知道如何停止它的人使用RServe。

2
如果无法在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 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接