这是关于 的讨论,请参阅:https://discuss.python.org/t/speed-up-shutil-copytree/62078。如果您有任何想法,请发送给我!
shutil 是 Python 中一个非常有用的模块。你可以在github中找到它:https://github.com/python/cpython/blob/master/Lib/shutil.py
shutil.copytree 是将一个文件夹复制到另一个文件夹的函数。
在该函数中,调用_copytree函数进行复制。
_copytree 有什么作用?
_当文件数量较多或文件大小较大时,copytree速度不是很快。
在这里测试:
import os import shutil os.mkdir('test') os.mkdir('test/source') def bench_mark(func, *args): import time start = time.time() func(*args) end = time.time() print(f'{func.__name__} takes {end - start} seconds') return end - start # write in 3000 files def write_in_5000_files(): for i in range(5000): with open(f'test/source/{i}.txt', 'w') as f: f.write('Hello World' + os.urandom(24).hex()) f.close() bench_mark(write_in_5000_files) def copy(): shutil.copytree('test/source', 'test/destination') bench_mark(copy)
结果是:
write_in_5000_files 需要 4.084963083267212 秒
复制需要 27.12768316268921 秒
我使用多线程来加速复制过程。我将函数重命名为_copytree_single_threaded,添加一个新函数_copytree_multithreaded。这是copytree_multithreaded:
def _copytree_multithreaded(src, dst, symlinks=False, ignore=None, copy_function=shutil.copy2, ignore_dangling_symlinks=False, dirs_exist_ok=False, max_workers=4): """Recursively copy a directory tree using multiple threads.""" sys.audit("shutil.copytree", src, dst) # get the entries to copy entries = list(os.scandir(src)) # make the pool with ThreadPoolExecutor(max_workers=max_workers) as executor: # submit the tasks futures = [ executor.submit(_copytree_single_threaded, entries=[entry], src=src, dst=dst, symlinks=symlinks, ignore=ignore, copy_function=copy_function, ignore_dangling_symlinks=ignore_dangling_symlinks, dirs_exist_ok=dirs_exist_ok) for entry in entries ] # wait for the tasks for future in as_completed(futures): try: future.result() except Exception as e: print(f"Failed to copy: {e}") raise
我添加了一个判断,选择是否使用多线程。
if len(entries) >= 100 or sum(os.path.getsize(entry.path) for entry in entries) >= 100*1024*1024: # multithreaded version return _copytree_multithreaded(src, dst, symlinks=symlinks, ignore=ignore, copy_function=copy_function, ignore_dangling_symlinks=ignore_dangling_symlinks, dirs_exist_ok=dirs_exist_ok) else: # single threaded version return _copytree_single_threaded(entries=entries, src=src, dst=dst, symlinks=symlinks, ignore=ignore, copy_function=copy_function, ignore_dangling_symlinks=ignore_dangling_symlinks, dirs_exist_ok=dirs_exist_ok)
我在源文件夹中写入了 50000 个文件。基准标记:
def bench_mark(func, *args): import time start = time.perf_counter() func(*args) end = time.perf_counter() print(f"{func.__name__} costs {end - start}s")
写入:
import os os.mkdir("Test") os.mkdir("Test/source") # write in 50000 files def write_in_file(): for i in range(50000): with open(f"Test/source/{i}.txt", 'w') as f: f.write(f"{i}") f.close()
两个比较:
def copy1(): import shutil shutil.copytree('test/source', 'test/destination1') def copy2(): import my_shutil my_shutil.copytree('test/source', 'test/destination2')
副本 1 花费 173.04780609999943s
copy2 花费 155.81321870000102s
copy2 比 copy1 快很多。你可以跑很多次。
使用多线程可以加快复制过程。但会增加内存占用。但我们不需要在代码中重写多线程。
感谢“巴里·斯科特”。我会听从他/她的建议:
通过使用异步 I/O,您可能会以更少的开销获得相同的改进。
我写了这些代码:
import os import shutil import asyncio from concurrent.futures import ThreadPoolExecutor import time # create directory def create_target_directory(dst): os.makedirs(dst, exist_ok=True) # copy 1 file async def copy_file_async(src, dst): loop = asyncio.get_event_loop() await loop.run_in_executor(None, shutil.copy2, src, dst) # copy directory async def copy_directory_async(src, dst, symlinks=False, ignore=None, dirs_exist_ok=False): entries = os.scandir(src) create_target_directory(dst) tasks = [] for entry in entries: src_path = entry.path dst_path = os.path.join(dst, entry.name) if entry.is_dir(follow_symlinks=not symlinks): tasks.append(copy_directory_async(src_path, dst_path, symlinks, ignore, dirs_exist_ok)) else: tasks.append(copy_file_async(src_path, dst_path)) await asyncio.gather(*tasks) # choose copy method def choose_copy_method(entries, src, dst, **kwargs): if len(entries) >= 100 or sum(os.path.getsize(entry.path) for entry in entries) >= 100 * 1024 * 1024: # async version asyncio.run(copy_directory_async(src, dst, **kwargs)) else: # single thread version shutil.copytree(src, dst, **kwargs) # test function def bench_mark(func, *args): start = time.perf_counter() func(*args) end = time.perf_counter() print(f"{func.__name__} costs {end - start:.2f}s") # write in 50000 files def write_in_50000_files(): for i in range(50000): with open(f"Test/source/{i}.txt", 'w') as f: f.write(f"{i}") def main(): os.makedirs('Test/source', exist_ok=True) write_in_50000_files() # 单线程复制 def copy1(): shutil.copytree('Test/source', 'Test/destination1') def copy2(): shutil.copytree('Test/source', 'Test/destination2') # async def copy3(): entries = list(os.scandir('Test/source')) choose_copy_method(entries, 'Test/source', 'Test/destination3') bench_mark(copy1) bench_mark(copy2) bench_mark(copy3) shutil.rmtree('Test') if __name__ == "__main__": main()
输出:
副本 1 花费 187.21 秒
copy2 花费 244.33s
copy3 花费 111.27 秒
可以看到异步版本比单线程版本更快。但单线程版本比多线程版本更快。 (可能是我的测试环境不太好,你可以尝试一下,把你的结果回复给我)
谢谢巴里·斯科特!
异步是一个不错的选择。但没有一个解决方案是完美的。如果您发现任何问题,可以回复我。
这是我第一次在 python.org 上写讨论。如果有任何问题,请告诉我。谢谢。
我的Github:https://github.com/mengqinyuan
我的开发者:https://dev.to/mengqinyuan
以上是加快 `shutil.copytree` 速度!的详细内容。更多信息请关注PHP中文网其他相关文章!