当前位置: 代码迷 >> 综合 >> 初学Python:多线程脚本-使用Thread类创建(from threading import Thread)
  详细解决方案

初学Python:多线程脚本-使用Thread类创建(from threading import Thread)

热度:21   发布时间:2024-01-25 00:19:12.0
#! /usr/bin/python
# -*- coding:utf-8 -*-'''
------------------------------------------
function:
多线程复制图片author: bingo
created: 2020-01-03
------------------------------------------
'''from Queue import Queue
import threading
import shutil
import os
import timecount = 0
DEFAULT_THREAD_NUM = 10file_dir = './11-bak'
fl = 'f.list'
target_dir = './11_copy/'def ensure_dir_exits(path):try:os.makedirs(path)except OSError:if not os.path.isdir(path):raisedef generate_list(file_dir):global countf = open(fl, 'w+')for root, dirs, files in os.walk(file_dir):for each in files:count += 1f_name = os.path.join(root,each)f.write(f_name + '\n')f.close()def generate_file_copy_func():def file_copy_func(file1):file2 = target_dir  + os.path.basename(file1) shutil.copy(file1, file2)return file_copy_funcdef work(que, func):while True:arg = que.get()func(arg)if que.qsize()%100 == 0:print (count-que.qsize())que.task_done()def run(File_l, func):que = Queue()f = open(File_l, 'r')for line in f:l = line.strip()que.put(l)for num in range(DEFAULT_THREAD_NUM):t = threading.Thread(target = work,args = (que,func))t.daemon = True 
'''
个人理解:daemon如果默认false,work()函数中会一直无限循环,不会结束,task_done和join()配合使用只是通知队列任务已经全部完成;设置成true以后,队列处理完,子线程会停止
'''t.start()while que.unfinished_tasks > 0:time.sleep(1)que.join()def main():ensure_dir_exits(target_dir)generate_list(file_dir)func = generate_file_copy_func()run(fl,func)main()
  相关解决方案