个人比较习惯这种写法
举个简单的场景:
有一批图片和图片对应的list,现在需要将符合特定要求的图片拷贝到另一个路径下,采用多线程处理,代码如下(备注:只展示简单的代码框架,主要是理解queue和threading的写法,具体内容还得靠自己写):
使用Thread类创建,给构造函数传递回调对象
# /usr/bin/python
# -*- coding:utf-8 -*-
import os, json, time
from Queue import Queue
import threading, shutil
pic_list = 'pic.list'
target_dir = 'target_pic'
DEFAULT_THREAD_NUM = 20
#######################################
#文件夹是否存在
def wheather_dir_exists(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
def generate_dict():
m = 0
dict1 = {}
dict2 = {}
...
此处代码省略
...
return dict1
#定义一个函数,函数里再定义个拷贝图片函数,调用generate_copy_func()可以返回一个拷贝图片函数
def generate_copy_func():
global pic_dir
global target_dir
def copy_func(value, dict3):