日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tornado总结7-文件切片异步非阻塞下载

發布時間:2025/3/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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-文件切片异步非阻塞下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。