#! /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()
详细解决方案
初学Python:多线程脚本-使用Thread类创建(from threading import Thread)
热度:21 发布时间:2024-01-25 00:19:12.0
相关解决方案
- Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml解决办法
- import cn.luxh.struts2.entity.Novel 急需的jar包是哪个
- thread(线程)中用spring注入不成功有关问题
- Exception in thread "main" java.lang.UnsupportedClassVersionError:有关问题
- Thread.sleep() 有关问题
- 配置j2me eclipse环境时,import WTK,在refresh的时候,devices什么都没有引入.该怎么解决
- Exception in thread "main" java.lang.NullPointerException,该怎么处理
- 小弟我的程序中Exception in thread "main" java.lang.NullPointerException如何解决
- The import cannot be resolved 有关问题
- The import javax.bluetooth cannot be resolved解决办法
- Exception in thread "main" java.lang.UnsatisfiedLinkError: com.
- Exception in thread "main" java.lang.InstantiationError:
- Exception in thread "Thread-7" java.lang.UnsatisfiedLinkError:
- import form.addBookForm 有晓得这个包的出处的嘛?SOS~
- Exception in thread "main" java.lang.NoClassDefFound:HelloWorld,该怎么处理
- 这个是什么东东 Pool thread stack traces
- import com.drew.metadata.怎么使用
- 导入一个项目后出现The import java.io cannot be resolved异常
- in thread "main" java.lang.OutOfMemoryError: Java heap space
- Exception in thread "main"不知名的异常!
- Exception in thread "main" java.security.InvalidKeyException:
- 上面的代码会出错,Exception in thread "main" java.lang.Error:
- import com.borland.jbcl.layout.解决办法
- Thread.sleep() 与 Thread.currentThread().sleep()差异
- 报错 Exception in thread "main" java.lang.NoClassDefFound:HelloWorld,该怎么解决
- Which methods guarantee that a thread will leave the running state解决方案
- 出现Exception in thread "main" java.lang.NullPointerException求赐教,
- Exception in thread "main" java.lang.NullPointerException求大牛帮忙解决,30行小程序解决方案
- Exception in thread "main" java.lang.NullPointerException如何解决
- java thread wait的用法解决方案