重新学习python线程应用使用thread类,改写程序
[*]#!/usr/bin/envpython
[*]#-*-coding:cp936-*-
[*]#===============================================================================
[*]#1、将原程序改为多线程
[*]#2、加入线程之间的互斥和锁操作
[*]#3、当其中一个线程执行完成后通知锁对象将该线程释放
[*]#===============================================================================
[*]importos,time,thread
[*]#Begin_set_workpath原地址路径
[*]Begin_set_workpath=r"D:\Python_dev"
[*]#Send_set_workpath复制到地址
[*]Send_set_workpath_1=r"D:\test\test1"
[*]Send_set_workpath_2=r"D:\test\test2"
[*]#Log_set_worpath日志生成地址
[*]Log_set_workpath=r"D:\Log_out.log"
[*]print"日志输出路径:%s"%(Log_set_workpath)
[*]newpath=""
[*]print"程序初始化完成,数据集加载成功!"
[*]
[*]loops=
[*]
[*]defCopy_method(newpath,path,lock):
[*]
[*]time.time()
[*]os.system("xcopy%s%s/y"%(newpath,path))
[*]lock.release()
[*]
[*]defmain():
[*]
[*]forroot,dirs,filesinos.walk(Begin_set_workpath):
[*]
[*]locks=[]
[*]nloops=range(len(loops))
[*]
[*]foriin(0,1):
[*]lock=thread.allocate_lock()
[*]lock.acquire()
[*]locks.append(lock)
[*]
[*]foriin(0,1):
[*]forflinfiles:
[*]#获得复制文件的文件路径
[*]newpath=os.path.join(root,fl)
[*]#循环创建线程
[*]thread.start_new_thread(Copy_method,(newpath,loops,locks))
[*]#输出日志
[*]Log_out=open(Log_set_workpath,'w')
[*]#日志中记录当前运行时间
[*]record_nowtime=time.localtime()
[*]Log_out.close()
[*]
[*]foriin(0,1):
[*]whilelocks.locked():pass
[*]
[*]if__name__=="__main__":
[*]main()
页:
[1]