使用 HTTP.jl 发送大文件
Send a large file with HTTP.jl
我想用 HTTP.jl 和 julia 实现一个服务器。经过一些计算,服务器将 return 一个 "large" 文件(大约几百 MB)。我想避免必须读取内存中的所有文件然后将其发送给客户端。
一些框架允许为此提供特定功能(例如 Flask http://flask.pocoo.org/docs/0.12/api/#flask.send_file) or allow to stream the content to the client (http://flask.pocoo.org/docs/0.12/patterns/streaming/)。
HTTP.jl 中是否也提供这两个选项之一?或者任何其他 Julia 网络包?
这是读取文件 testfile.txt
的测试代码,但我想避免将整个文件加载到内存中。
import HTTP
f = open("testfile.txt","w")
write(f,"test")
close(f)
router = HTTP.Router()
function testfun(req::HTTP.Request)
f = open("testfile.txt")
data = read(f)
close(f)
return HTTP.Response(200,data)
end
HTTP.register!(router, "GET", "/testfun",HTTP.HandlerFunction(testfun))
server = HTTP.Servers.Server(router)
task = @async HTTP.serve(server, ip"127.0.0.1", 8000; verbose=false)
sleep(1.0)
req = HTTP.request("GET","http://127.0.0.1:8000/testfun/")
# end server
put!(server.in, HTTP.Servers.KILL)
@show String(req.body)
您可以这样使用 memory mapped IO:
function testfun(req::HTTP.Request)
data = Mmap.mmap(open("testfile.txt"), Array{UInt8,1})
return HTTP.Response(200,data)
end
data
现在对于 julia 来说看起来像一个普通的字节数组,但实际上对文件来说是喜欢的,这可能正是您想要的。该文件将在垃圾收集时关闭 - 如果您有很多请求并且没有触发垃圾收集,您可能最终会打开很多文件。如果您的请求需要很长时间,您可以考虑在请求开始时调用 gc()。
我想用 HTTP.jl 和 julia 实现一个服务器。经过一些计算,服务器将 return 一个 "large" 文件(大约几百 MB)。我想避免必须读取内存中的所有文件然后将其发送给客户端。
一些框架允许为此提供特定功能(例如 Flask http://flask.pocoo.org/docs/0.12/api/#flask.send_file) or allow to stream the content to the client (http://flask.pocoo.org/docs/0.12/patterns/streaming/)。
HTTP.jl 中是否也提供这两个选项之一?或者任何其他 Julia 网络包?
这是读取文件 testfile.txt
的测试代码,但我想避免将整个文件加载到内存中。
import HTTP
f = open("testfile.txt","w")
write(f,"test")
close(f)
router = HTTP.Router()
function testfun(req::HTTP.Request)
f = open("testfile.txt")
data = read(f)
close(f)
return HTTP.Response(200,data)
end
HTTP.register!(router, "GET", "/testfun",HTTP.HandlerFunction(testfun))
server = HTTP.Servers.Server(router)
task = @async HTTP.serve(server, ip"127.0.0.1", 8000; verbose=false)
sleep(1.0)
req = HTTP.request("GET","http://127.0.0.1:8000/testfun/")
# end server
put!(server.in, HTTP.Servers.KILL)
@show String(req.body)
您可以这样使用 memory mapped IO:
function testfun(req::HTTP.Request)
data = Mmap.mmap(open("testfile.txt"), Array{UInt8,1})
return HTTP.Response(200,data)
end
data
现在对于 julia 来说看起来像一个普通的字节数组,但实际上对文件来说是喜欢的,这可能正是您想要的。该文件将在垃圾收集时关闭 - 如果您有很多请求并且没有触发垃圾收集,您可能最终会打开很多文件。如果您的请求需要很长时间,您可以考虑在请求开始时调用 gc()。