Chinaunix首页 | 论坛 | 博客
  • 博客访问: 382663
  • 博文数量: 58
  • 博客积分: 2941
  • 博客等级: 少校
  • 技术积分: 970
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 11:37
文章分类

全部博文(58)

文章存档

2015年(1)

2014年(1)

2012年(1)

2011年(19)

2009年(1)

2008年(1)

2007年(11)

2006年(10)

2005年(13)

分类: Python/Ruby

2011-03-22 12:13:38

最近在学习PYTHON的多线程操作,写个复制文件带进度条的小程序玩,使用的是threading模块中类方法的多线程方式.linux系统中的cp命令与PYTHON的shutil模块都会导致复制大文件时高负载,处理大文件时感觉还需要再优化下程序.
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. from __future__ import division
  4. import threading
  5. import time
  6. import os
  7. import sys
  8. #date = time.strftime('%Y%m%d')
  9. class A(threading.Thread):
  10.         def __init__(self):
  11.                 threading.Thread.__init__(self)
  12.         def run(self):
  13.                 os.system("cp test.txt test.txt.1")
  14. class B(threading.Thread):
  15.         def __init__(self):
  16.                 threading.Thread.__init__(self)
  17.         def run(self):
  18.                 global x
  19.                 global y
  20.                 x = 0
  21.                 if os.path.isfile("test.txt"):
  22.                         y = os.stat("test.txt")[6]
  23.                 while x < y:
  24.                         if os.path.isfile("test.txt.1"):
  25.                                 x = os.stat("test.txt.1")[6]
  26.                         else:
  27.                                 #print 1
  28.                                 x = os.stat("test.txt.1")[6]
  29.                         b = 0
  30.                         a = int((x / y) * 100)
  31.                         b = int(a - b)
  32.                         j = '#' * int(b / 2)
  33.                         sys.stdout.write(str(a)+"% ||"+j+"||"+"\r")
  34.                         sys.stdout.flush()
  35.                         time.sleep(2)
  36.                 print
  37. def test():
  38.         threadone = A()
  39.         threadtwo = B()
  40.         threadone.start()
  41.         time.sleep(1)
  42.         threadtwo.start()
  43. if __name__ == '__main__':
  44.         test()
阅读(4979) | 评论(0) | 转发(0) |
0

上一篇:ping测试脚本

下一篇:python的线程锁机制

给主人留下些什么吧!~~