所以我有用户从django服务器请求他们的文件,在django服务器中,对请求的文件进行一些处理。目前我正在将文件写入磁盘并从那里提供服务。在
为了提高效率和性能(使用x-sendfile),有没有从内存直接将文件提供给用户?在
以下是我目前的看法:
def ServeSegment(request, segmentID):
segments =[]
obj = MainFile.objects.filter(owner=request.user)
file_name = MainFile.objects.get(file_id=segmentID).file_name
file_suffix = file_name = MainFile.objects.get(file_id=segmentID).file_suffix
if request.method == 'GET':
hosts = settings.HOSTS
for i in hosts:
try:
url = 'http://' + i + ':8000/foo/'+str(segmentID)
r = requests.get(url, timeout=1, stream=True)
if r.status_code == 200:
segments.append(r.content)
except:
continue
instance = SeIDA(filename='test', x=settings.M, y=settings.N)
docfile = instance.decoder(segments)
with open('/tmp/Files/'+file_name, 'wb') as f:
f.write(docfile)
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename={0}'.format(file_name)
response['X-Sendfile'] = "/tmp/Files/{0}".format(file_name)
return response
注:正在使用的SeIDA模块将数据编码为N个片段,这样M个片段就足以构建数据。因此,在上面的视图中,我从存储服务器检索段并将它们组合起来。在
我的问题是:如何在不保存文档的情况下直接提供文档文件。有吗?在
您不能使用X-Sendfile从内存中为它提供服务,因为Apache运行在一个不同的进程中,它甚至在一个完全不同的机器上运行。在
但是在这种情况下,使用sendfile并不能真正提高效率。既然您所说的文件已经在内存中,您应该直接从那里提供它,而不需要通过Apache:只需在响应中返回它。在
相关问题 更多 >
编程相关推荐