Removing tmp file after return HttpResponse in django

后端 未结 6 658
长情又很酷
长情又很酷 2020-12-11 03:06

I\'m using the following django/python code to stream a file to the browser:

wrapper = FileWrapper(file(path))
response = HttpResponse(wrapper, content_type=         


        
6条回答
  •  粉色の甜心
    2020-12-11 04:07

    Mostly, we use periodic cron jobs for this.

    Django already has one cron job to clean up lost sessions. And you're already running it, right?

    See http://docs.djangoproject.com/en/dev/topics/http/sessions/#clearing-the-session-table

    You want another command just like this one, in your application, that cleans up old files.

    See this http://docs.djangoproject.com/en/dev/howto/custom-management-commands/

    Also, you may not really be sending this file from Django. Sometimes you can get better performance by creating the file in a directory used by Apache and redirecting to a URL so the file can be served by Apache for you. Sometimes this is faster. It doesn't handle the cleanup any better, however.

提交回复
热议问题