1

从这里继续:https ://codereview.stackexchange.com/questions/

我发现在列表中保存数据字节可能很昂贵并且可能会降低系统性能,所以我认为最好在该线程的范围完成后立即将数据写入磁盘,因此其余的块完成应该写入来自队列中下一个线程完成的范围块..

def main(url=None, splitBy=6):
    start_time = time.time()
    if not url:
        print "Please Enter some url to begin download."
        return

    fileName = url.split('/')[-1]
    sizeInBytes = requests.head(url, headers={'Accept-Encoding': 'identity'}).headers.get('content-length', None)
    print "%s bytes to download." % sizeInBytes
    if not sizeInBytes:
        print "Size cannot be determined."
        return
    threads = []
    lock = threading.Lock()

    byteRanges = buildRange(int(sizeInBytes), splitBy)
    for idx in range(splitBy):
        bufTh = SplitBufferThread(url, byteRanges[idx])
        bufTh.daemon = True
        bufTh.start()
        threads.append(bufTh)

    print "--- %s seconds ---" % str(time.time() - start_time)

    with open(fileName, 'w+') as fh:
        for t in threads:
            t.join()
            fh.write(t.data)
        fh.flush()

    print "Finished Writing file %s" % fileName

if __name__ == '__main__':
    main(url)

那么如何在线程完成字节范围后立即将文件块写入磁盘?

4

0 回答 0