多线程文件复制比多核CPU上的单个线程慢得多

ste*_*reo 3 python queue multithreading copy file

我试图用Python编写一个多线程程序来加速(低于1000).csv文件的复制.多线程代码比顺序方法运行得更慢.我用时间计算代码profile.py.我相信我一定做错了什么,但我不确定是什么.

环境:

  • 四核CPU.
  • 2个硬盘,一个包含源文件.另一个是目的地.
  • 1000个csv文件,大小从几KB到10 MB不等.

该方法:

我将所有文件路径放在一个队列中,并创建4-8个工作线程从队列中拉出文件路径并复制指定的文件.在任何情况下,多线程代码都不会更快:

  • 顺序复制需要150-160秒
  • 线程副本需要230秒

我假设这是一个I/O绑定任务,因此多线程应该有助于操作速度.

代码:

    import Queue
    import threading
    import cStringIO 
    import os
    import shutil
    import timeit  # time the code exec with gc disable
    import glob    # file wildcards list, glob.glob('*.py')
    import profile # 

    fileQueue = Queue.Queue() # global
    srcPath  = 'C:\\temp'
    destPath = 'D:\\temp'
    tcnt = 0
    ttotal = 0

    def CopyWorker():
        while True:
            fileName = fileQueue.get()
            fileQueue.task_done()
            shutil.copy(fileName, destPath)
            #tcnt += 1
            print 'copied: ', tcnt, ' of ', ttotal

    def threadWorkerCopy(fileNameList):
        print 'threadWorkerCopy: ', len(fileNameList)
        ttotal = len(fileNameList)
        for i in range(4):
            t = threading.Thread(target=CopyWorker)
            t.daemon = True
            t.start()
        for fileName in fileNameList:
            fileQueue.put(fileName)
        fileQueue.join()

    def sequentialCopy(fileNameList):
        #around 160.446 seconds, 152 seconds
        print 'sequentialCopy: ', len(fileNameList)
        cnt = 0
        ctotal = len(fileNameList)
        for fileName in fileNameList:
            shutil.copy(fileName, destPath)
            cnt += 1
            print 'copied: ', cnt, ' of ', ctotal

    def main():
        print 'this is main method'
        fileCount = 0
        fileList = glob.glob(srcPath + '\\' + '*.csv')
        #sequentialCopy(fileList)
        threadWorkerCopy(fileList)

    if __name__ == '__main__':
        profile.run('main()')
Run Code Online (Sandbox Code Playgroud)

Dav*_*rtz 10

当然它比较慢.硬盘驱动器不得不在文件之间不断寻找.您认为多线程会使这项任务更快,这是完全没有道理的.限制速度是指您从磁盘读取数据或将数据写入磁盘的速度,每次从一个文件到另一个文件的搜索都是可能花费在传输数据上的时间.