tornado总结7-文件切片异步非阻塞下载
生活随笔
收集整理的這篇文章主要介紹了
tornado总结7-文件切片异步非阻塞下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
目標
? 文件切片,每一片都進行異步下載
? 影響是增加了文件的下載時間
?
代碼
import os import statimport tornado.gen import tornado.httpserver import tornado.ioloop import tornado.web from tornado import iostreamclass DownloadHandler(tornado.web.RequestHandler):@tornado.gen.coroutinedef get(self):file_path = './a.zip'content_length = self.get_content_size(file_path)self.set_header("Content-Length", content_length)self.set_header("Content-Type", "application/octet-stream")self.set_header("Content-Disposition", "attachment;filename=\"{0}\"".format("a.data")) #設置新的文件名content = self.get_content(file_path)if isinstance(content, bytes):content = [content]for chunk in content:try:self.write(chunk)yield self.flush()except iostream.StreamClosedError:breakreturn# 使用python自帶的對于yield的應用對文件進行切片,for循環每運用一次就調用一次def get_content(self, file_path):start=Noneend=Nonewith open(file_path, "rb") as file:if start is not None:file.seek(start)if end is not None:remaining = end - (start or 0)else:remaining = Nonewhile True:chunk_size = 64 * 1024 #每片的大小是64Kif remaining is not None and remaining < chunk_size:chunk_size = remainingchunk = file.read(chunk_size)if chunk:if remaining is not None:remaining -= len(chunk)yield chunkelse:if remaining is not None:assert remaining == 0return# 讀取文件長度def get_content_size(self, file_path):stat_result = os.stat(file_path)content_size = stat_result[stat.ST_SIZE]return content_sizeclass HelloHandler(tornado.web.RequestHandler):def get(self):return self.finish("Hello Tornado")class Application(tornado.web.Application):def __init__(self):handlers = [(r"/download", DownloadHandler),(r".*?", HelloHandler),]tornado.web.Application.__init__(self, handlers)if __name__ == "__main__":port = 8899application = Application()http_server = tornado.httpserver.HTTPServer(application, xheaders=True)http_server.listen(port)print('Listen on http://localhost:{0}'.format(port))tornado.ioloop.IOLoop.instance().start()"/download"路由的Get方法調用時,將會進行一個文件下載操作,將同目錄下的a.zip文件切片下載為a.data文件。
下載過程中,會空出一些時間間隙,讓tornado繼續處理其他類型的請求。
下載過程綜合了python和tornado對yield的應用。
?
?
轉載于:https://my.oschina.net/u/111188/blog/677987
總結
以上是生活随笔為你收集整理的tornado总结7-文件切片异步非阻塞下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开始学习马哥的linux
- 下一篇: ThreadLocal_OSIV模式_F