最近在学习PYTHON的多线程操作,写个复制文件带进度条的小程序玩,使用的是threading模块中类方法的多线程方式.linux系统中的cp命令与PYTHON的shutil模块都会导致复制大文件时高负载,处理大文件时感觉还需要再优化下程序.
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- from __future__ import division
- import threading
- import time
- import os
- import sys
- #date = time.strftime('%Y%m%d')
- class A(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- def run(self):
- os.system("cp test.txt test.txt.1")
- class B(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- def run(self):
- global x
- global y
- x = 0
- if os.path.isfile("test.txt"):
- y = os.stat("test.txt")[6]
- while x < y:
- if os.path.isfile("test.txt.1"):
- x = os.stat("test.txt.1")[6]
- else:
- #print 1
- x = os.stat("test.txt.1")[6]
- b = 0
- a = int((x / y) * 100)
- b = int(a - b)
- j = '#' * int(b / 2)
- sys.stdout.write(str(a)+"% ||"+j+"||"+"\r")
- sys.stdout.flush()
- time.sleep(2)
- print
- def test():
- threadone = A()
- threadtwo = B()
- threadone.start()
- time.sleep(1)
- threadtwo.start()
- if __name__ == '__main__':
- test()
阅读(5047) | 评论(0) | 转发(0) |