我在 R 中使用 Rook 创建了一个服务器 - http://cran.r-project.org/web/packages/Rook 代码如下
#!/usr/bin/Rscript
library(Rook)
s <- Rhttpd$new()
s$add(
name="pingpong",
app=Rook::URLMap$new(
'/ping' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Pong</a></h1>',req$to_url("/pong")))
res$finish()
},
'/pong' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$write(sprintf('<h1><a href="%s">Ping</a></h1>',req$to_url("/ping")))
res$finish()
},
'/?' = function(env){
req <- Rook::Request$new(env)
res <- Rook::Response$new()
res$redirect(req$to_url('/pong'))
res$finish()
}
)
)
## Not run:
s$start(port=9000)
$ ./Rook.r
Loading required package: tools
Loading required package: methods
Loading required package: brew
starting httpd help server ... done
Server started on host 127.0.0.1 and port 9000 . App urls are:
http://127.0.0.1:9000/custom/pingpong
Server started on 127.0.0.1:9000
[1] pingpong http://127.0.0.1:9000/custom/pingpong
Call browse() with an index number or name to run an application.
$
该过程到此结束。
它在 R shell 中运行良好,但我想在系统启动时将它作为服务器运行。因此,一旦调用了 start ,R 就不应退出,而是等待端口上的请求。我将如何说服 R 只是等待或睡眠而不是退出?我可以使用 R 中的等待或睡眠功能等待 N 秒,但这并不完全符合要求