基于线程池的大目录拷贝
时间: 2023-11-14 11:08:53 浏览: 41
以下是基于线程池的大目录拷贝的示例代码:
```python
import os
import shutil
import threading
class CopyTask:
def __init__(self, src, dst):
self.src = src
self.dst = dst
def run(self):
shutil.copytree(self.src, self.dst)
print(f"Copy from {self.src} to {self.dst} completed.")
class ThreadPool:
def __init__(self, num_threads):
self.num_threads = num_threads
self.task_queue = []
self.threads = []
def start(self):
for i in range(self.num_threads):
thread = threading.Thread(target=self.run)
self.threads.append(thread)
thread.start()
def run(self):
while True:
task = None
with threading.Lock():
if self.task_queue:
task = self.task_queue.pop(0)
if task:
task.run()
else:
break
def submit(self, task):
with threading.Lock():
self.task_queue.append(task)
def join(self):
for thread in self.threads:
thread.join()
if __name__ == "__main__":
src = "/path/to/source"
dst = "/path/to/destination"
num_threads = 4
# Create thread pool and start threads
pool = ThreadPool(num_threads)
pool.start()
# Submit copy tasks to thread pool
for dirpath, dirnames, filenames in os.walk(src):
for dirname in dirnames:
src_path = os.path.join(dirpath, dirname)
dst_path = src_path.replace(src, dst)
task = CopyTask(src_path, dst_path)
pool.submit(task)
# Wait for all tasks to complete
pool.join()
print("All copy tasks completed.")
```
此代码使用了一个`CopyTask`类来表示一个目录拷贝任务,该任务包含源目录和目标目录。线程池类`ThreadPool`包含一个任务队列和一组线程,可以提交任务以异步执行。在主函数中,使用`os.walk`遍历源目录中的所有目录,并将每个目录拷贝任务提交到线程池中。最后,等待所有任务完成后,输出完成消息。