`shutil.copytree` を高速化します!

WBOY
リリース: 2024-08-28 18:32:06
オリジナル
397 人が閲覧しました

Speed up `shutil.copytree` !

shutil.copytree の高速化について議論する

ここに書いてください

これは に関するディスカッションです。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 は何をしますか?

  1. 指定されたファイル/ディレクトリを無視します。
  2. 宛先ディレクトリを作成しています。
  3. シンボリック リンクの処理中にファイルまたはディレクトリをコピーします。
  4. 発生したエラー (権限の問題など) を収集し、最終的に発生させます。
  5. ソース ディレクトリのメタデータを宛先ディレクトリに複製します。

問題点

ファイル数が多い場合やファイルサイズが大きい場合、_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()

ログイン後にコピー

2 つの比較:

def copy1():
    import shutil
    shutil.copytree('test/source', 'test/destination1')

def copy2():
    import my_shutil
    my_shutil.copytree('test/source', 'test/destination2')

ログイン後にコピー
  • 「my_shutil」は私の修正版 shutil です。

コピー 1 のコストは 173.04780609999943 秒
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()
ログイン後にコピー

出力:

copy1 のコストは 187.21 秒
copy2 のコストは 244.33 秒
copy3 のコストは 111.27 秒


非同期バージョンはシングルスレッドバージョンよりも高速であることがわかります。ただし、シングル スレッド バージョンはマルチスレッド バージョンよりも高速です。 (私のテスト環境があまり良くないかもしれません。試して結果を私に返信して送ってください)

バリー・スコット、ありがとう!

メリットとデメリット

非同期は良い選択です。しかし、完璧な解決策はありません。何か問題が見つかった場合は、返信として私に送ってください。

終わり

python.org でディスカッションを書くのはこれが初めてです。何か問題がございましたら、お知らせください。ありがとうございます。

私の Github: https://github.com/mengqinyuan
私の開発者: https://dev.to/mengqinyuan

以上が`shutil.copytree` を高速化します!の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!