简体   繁体   English

OpenCPU:重用前一个会话的结果不起作用

[英]OpenCPU: Reuse results from previous session not working

I would like to use save_iris_as_csv to save a .csv file. 我想使用save_iris_as_csv来保存.csv文件。 Then, call catch_url_and_download to download this .csv file using the session returned by save_iris_as_csv . 然后,使用save_iris_as_csv返回的会话调用catch_url_and_download下载此.csv文件。 However, the catch_url_and_download does not work, returning an error of 但是, catch_url_and_download不起作用,返回错误

OpenCPU error HTTP 400
cannot open URL 'http://localhost:5656/ocpu/tmp/x06c27c3ac4/files/iris.csv'

The URL can be opened manually. 可以手动打开URL。 It is just the second R function cannot open it. 它只是第二个R功能无法打开它。

Following is my javascript code. 以下是我的javascript代码。

ocpu.call("save_iris_as_csv",{},function(session){
    console.log("save_iris_as_csv is good.")
    console.log(session)
    console.log("trying to call catch_url_and_download.")
    ocpu.call("catch_url_and_download",{
        url:session.loc + "files/iris.csv"
    },function(session2){
        console.log(session2)
    }).fail(function(e){
        console.log("catch_url_and_download failed. Cannot open URL xxx.")
        alert(e.responseText)
    })
})

Following is my R codes. 以下是我的R代码。

save_iris_as_csv = function(){
  write.csv(iris,"iris.csv")
}
catch_url_and_download = function(url){
  download.file(url,"iris.csv")
}

I am using Windows OS. 我正在使用Windows操作系统。

The problem here is that R is single threaded and hence the httpuv webserver (on which the opencpu single-user server is based) can only serve a single request at a time. 这里的问题是R是单线程的,因此httpuv webserver(opencpu单用户服务器所基于的)一次只能提供一个请求。 In your example you've created a grid lock. 在您的示例中,您创建了一个网格锁。

Your example makes a request to a function that then makes a second request to the same webserver using download.file() . 您的示例向函数发出请求,然后使用download.file()同一Web服务器发出第二个请求。 This second requests gets queued by httpuv to be served when the first one completes, but obviously that never happens because download.file() just keeps waiting, until it times out. 当第一个请求完成后,第二个请求将被httpuv排队,但显然从未发生,因为download.file()只是一直等待,直到它超时。

This does not happen in the cloud server because apache2 is multi threaded. 这在云服务器中不会​​发生,因为apache2是多线程的。

To make it work with the single user server, you can make the first function save the data as an object in the workspace, and then the second function can access it via the session key. 要使其与单用户服务器一起使用,您可以使第一个函数将数据保存为工作空间中的对象,然后第二个函数可以通过会话密钥访问它。 Or alternatively you could save it on disk or in a database or something, where the second function can access it. 或者,您可以将其保存在磁盘或数据库或其他内容中,第二个函数可以访问它。 Anything that doesn't need a second http connection to the same local server. 任何不需要第二个http连接到同一本地服务器的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM