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

#! /usr/bin/python
# -*- coding:utf-8 -*-

'''
------------------------------------------
function:
多线程复制图片

author: bingo
created: 2020-01-03
------------------------------------------
'''

from Queue import Queue
import threading
import shutil
import os
import time

count = 0
DEFAULT_THREAD_NUM = 10

file_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):
            raise

def generate_list(file_dir):
    global count
    f = open(fl, 'w+')
    for root, dirs, files in os.walk(file_dir):
        for each in files:
            count += 1
            f_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_func

def 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()
发布了37 篇原创文章 · 获赞 2 · 访问量 7602

猜你喜欢

转载自blog.csdn.net/bingozb/article/details/103964625