0

我正在尝试计算文件的哈希值以检查是否进行了任何更改。我有 Gui 和其他一些观察者在事件循环中运行。所以,我决定异步计算文件的哈希值 [md5/Sha1 哪个更快]。

同步代码:

import hashlib
import time


chunk_size = 4 * 1024

def getHash(filename):
    md5_hash = hashlib.md5()
    with open(filename, "rb") as f:
        for byte_block in iter(lambda: f.read(chunk_size), b""):
            md5_hash.update(byte_block)
        print("getHash : " + md5_hash.hexdigest())

start = time.time()
getHash("C:\\Users\\xxx\\video1.mkv")
getHash("C:\\Users\\xxx\\video2.mkv")
getHash("C:\\Users\\xxx\\video3.mkv")
end = time.time()

print(end - start)

同步代码的输出: 2.4000535011291504

异步代码:

import hashlib
import aiofiles
import asyncio
import time


chunk_size = 4 * 1024

async def get_hash_async(file_path: str):
    async with aiofiles.open(file_path, "rb") as fd:
        md5_hash = hashlib.md5()
        while True:
            chunk = await fd.read(chunk_size)
            if not chunk:
                break
            md5_hash.update(chunk)
        print("get_hash_async : " + md5_hash.hexdigest())

async def check():
    start = time.time()
    t1 = get_hash_async("C:\\Users\\xxx\\video1.mkv")
    t2 = get_hash_async("C:\\Users\\xxx\\video2.mkv")
    t3 = get_hash_async("C:\\Users\\xxx\\video3.mkv")
    await asyncio.gather(t1,t2,t3)
    end = time.time()
    print(end - start)

loop = asyncio.get_event_loop()
loop.run_until_complete(check())

异步代码的输出:27.957366943359375

我做对了吗?或者,是否需要进行任何更改以提高代码的性能?

提前致谢。

4

1 回答 1

1

在同步情况下,您按顺序读取文件。按块顺序读取文件更快。

在异步情况下,您的事件循环在计算哈希时会阻塞。这就是为什么只能同时计算一个哈希值的原因。术语“CPU 限制”和“I/O 限制”是什么意思?

如果要提高计算速度,则需要使用线程。线程可以在 CPU 上并行执行。增加 CHUNK_SIZE 也应该有所帮助。

import hashlib
import os
import time

from pathlib import Path
from multiprocessing.pool import ThreadPool


CHUNK_SIZE = 1024 * 1024


def get_hash(filename):
    md5_hash = hashlib.md5()
    with open(filename, "rb") as f:
        while True:
            chunk = f.read(CHUNK_SIZE)
            if not chunk:
                break
            md5_hash.update(chunk)
        return md5_hash


if __name__ == '__main__':
    directory = Path("your_dir")
    files = [path for path in directory.iterdir() if path.is_file()]

    number_of_workers = os.cpu_count()
    start = time.time()
    with ThreadPool(number_of_workers) as pool:
        files_hash = pool.map(get_hash, files)
    end = time.time()

    print(end - start)

如果只计算 1 个文件的哈希值:aiofiles 为每个文件使用一个线程。操作系统需要时间来创建线程。

于 2020-01-26T11:16:27.163 回答