小编ste*_*reo的帖子

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

我试图用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 …
Run Code Online (Sandbox Code Playgroud)

python queue multithreading copy file

3
推荐指数
1
解决办法
4583
查看次数

标签 统计

copy ×1

file ×1

multithreading ×1

python ×1

queue ×1