title: django: Is there anyway to serve a file directly from memory
body: <p>So I have users requesting their files from a django server inside which some processes take place on the requested file. Currently I am writing the file to the disk and serve it from there. </p>
  
  <p>Is there anyway to serve the file directly to the user from the memory for a better efficiency and performance (using x-sendfile)? </p>
  
  <p>Here is my current view:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Note</strong>: SeIDA module being used, encodes data into N segments such that M segments is sufficed to construct the data. So in the view above I am retrieving the segments from the storage servers and combining them.</p>
  
  <p>My question being: How can I directly serve the docfile without saving it. Is there anyway?</p>