multiproc_download.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:pylibs 作者: tqlihuiqi 项目源码 文件源码
def get(self):
        """ ???? """

        if self.readBytes < self.totalBytes:
            shards = range(self.totalBytes)

            os.mkdir(self.tempdir)

            for i in range(0, self.totalBytes, self.readBytes):
                point = shards[i: i + self.readBytes]
                headers = { "Range": "bytes=%s-%s" % (point[0], point[-1]) }
                filename = os.path.join(self.tempdir, str(point[-1]))

                thread = self.threadPool.get()
                t = thread(target=self.write_to_file, args=[filename, headers])
                t.start()

            while activeCount() > 1:
                time.sleep(1)

            results = sorted(map(int, os.listdir(self.tempdir)))

            with open(self.filename, "ab") as wfd:
                for result in results:
                    f = os.path.join(self.tempdir, str(result))

                    with open(f, "rb") as rfd:
                        content = True

                        while content:
                            content = rfd.read(1024)
                            wfd.write(content)

            self.delete_temp_dir()

        else:
            self.write_to_file(filename=self.filename, useThread=False)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号