2016-01-20 49 views

回答

2

一些試驗和錯誤後,下面是重啓服務器每5秒一個工作代碼:

library(httpuv) 

host <- '127.0.0.1' 

port <- 8080 

app <- list(
    call = function(req) { 
    list(
     status = 200L, 
     headers = list('Content-Type' = 'text/plain'), 
     body = 'Hello' 
    ) 
    } 
) 

run <- function(host, port, app, period) { 
    sv <- startServer(host, port, app) 
    on.exit(stopServer(sv)) 
    cat("Server started\n") 

    restart <- Sys.time() + period 

    while (TRUE) { 
    service() 
    Sys.sleep(0.001) 
    if (Sys.time() > restart) { 
     stopServer(sv) 
     sv <- startServer(host, port, app) 
     restart <- Sys.time() + period 
     cat("Server restarted\n") 
    } 
    } 
} 

run(host, port, app, 5)