- #!/usr/bin/env python
- #-*- coding: cp936 -*-
- #===============================================================================
- # 1、将原程序改为多线程
- # 2、加入线程之间的互斥和锁操作
- # 3、当其中一个线程执行完成后通知锁对象将该线程释放
- #===============================================================================
- import os,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 = [Send_set_workpath_1,Send_set_workpath_2]
- def Copy_method(newpath,path,lock):
- time.time()
- os.system("xcopy %s %s /y" % (newpath,path))
- lock.release()
- def main():
- for root, dirs, files in os.walk(Begin_set_workpath):
- locks = []
- nloops = range(len(loops))
- for i in (0,1):
- lock = thread.allocate_lock()
- lock.acquire()
- locks.append(lock)
- for i in (0,1):
- for fl in files:
- #获得复制文件的文件路径
- newpath = os.path.join(root,fl)
- #循环创建线程
- thread.start_new_thread(Copy_method,(newpath,loops[i],locks[i]))
- #输出日志
- Log_out = open(Log_set_workpath,'w')
- #日志中记录当前运行时间
- record_nowtime = time.localtime()
- Log_out.close()
- for i in (0,1):
- while locks[i].locked():pass
- if __name__ == "__main__":
- main()
重新学习python线程应用使用thread类,改写程序
最新推荐文章于 2022-11-16 18:01:56 发布