语境
为了测试我正在编写的 R 包的 Web 功能,我尝试使用该httpuv
包在本地提供文件,以便我可以使用页面的脱机副本运行测试。
问题
但是,curl
似乎不想玩得很好httpuv
- 特别是,当尝试使用curl
(例如,with curl::curl()
or curl::curl_fetch_memory()
)读取托管文件时,请求挂起,如果没有手动中断,最终会超时。
最小的例子
# Serve a small page
server <- httpuv::startServer("0.0.0.0", port = 9359, app = list(
call = function(req) {
list(
status = 200L,
headers = list("Content-Type" = "text/html"),
body = "Some content..."
)
}
))
# Attempt to retrieve content (this hangs)
page <- curl::curl_fetch_memory(url = "http://127.0.0.1:9359")
httpuv::stopServer(server)
现在的进展
服务器启动后,curl -v 127.0.0.1:9359
在终端上运行会按预期返回内容。此外,如果我打开一个新的 RStudio 实例并尝试curl::curl_fetch_memory()
在那个新的 R 会话中使用(而旧的会话仍然打开),它会完美运行。
受此鼓舞,我已经玩callr
了一段时间,想也许可以在某个后台进程中启动服务器,然后像往常一样继续。不幸的是,到目前为止,我在这种方法上还没有取得任何成功。
非常感谢任何见解或建议!