鱼C论坛

 找回密码
 立即注册
查看: 2899|回复: 3

[作品展示] python多进程( 永远删除不完的文件 )+ 进度条打印

[复制链接]
发表于 2020-7-31 15:29:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Cool_Breeze 于 2020-8-4 15:32 编辑
  1. # 永远删不完的文件
  2. # BY GIN
  3. # 2020-7-31

  4. #coding=utf-8

  5. import os
  6. from multiprocessing import Process, Queue
  7. import time
  8. import random

  9. PH = r'D:\GIN\py\test\temp'
  10. Q = Queue()

  11. def random_del():
  12.     # 随机删除一个文件
  13.     os.chdir(PH)
  14.     while True:
  15.         time.sleep(0.5)
  16.         file = str(random.Random.randint(random,1,10)) + '.txt'
  17.         if os.path.isfile(file):
  18.             print('随机删除一个文件:{}'.format(file))
  19.             os.remove(file)
  20.         

  21. def supplement_t(Q):
  22.     # 补充缺失文件
  23.     os.chdir(PH)
  24.     while True:
  25.         time.sleep(0.5)
  26.         # print('get {:>2}'.format(Q.qsize()))
  27.         while not Q.empty():
  28.             file = Q.get()
  29.             print('正在补充缺失文件:{}'.format(file))
  30.             with open(file, "w") as f:
  31.                 pass


  32. def chec_txt(Q):
  33.     # 检查文件是否缺失。
  34.     # 如果文件缺失,向补充文件函数发送
  35.     # 缺失文件。
  36.     os.chdir(PH)
  37.     while True:
  38.         for i in (range(1,11)):
  39.             time.sleep(0.3)
  40.             file = str(i) + '.txt'
  41.             if not os.path.isfile(file):
  42.                 Q.put(file)
  43.                 # print('put {:>2}'.format(Q.qsize()))
  44.    

  45. def generate_txt():
  46.     # 创建所有文件。
  47.     os.chdir(PH)
  48.     for i in range(1,11):
  49.         file = str(i) + '.txt'
  50.         print('touch  {:>6}'.format(file))
  51.         with open(file, "w") as f:
  52.             pass

  53.    
  54. if __name__ == '__main__':
  55.     gen_p = Process(target=generate_txt)
  56.     che_p = Process(target=chec_txt, args=(Q,))     # 需要传入相同的对象
  57.     get_p = Process(target=supplement_t, args=(Q,))   # 需要传入相同的对象
  58.     rad_p = Process(target=random_del)
  59.     gen_p.start()
  60.     che_p.start()
  61.     get_p.start()
  62.     rad_p.start()
  63.     # 主进程会等待所有进程结束
复制代码


result:
  1. touch   1.txt
  2. touch   2.txt
  3. touch   3.txt
  4. touch   4.txt
  5. touch   5.txt
  6. touch   6.txt
  7. touch   7.txt
  8. touch   8.txt
  9. touch   9.txt
  10. touch  10.txt
  11. 随机删除一个文件:5.txt
  12. 随机删除一个文件:6.txt
  13. 随机删除一个文件:1.txt
  14. 正在补充缺失文件:5.txt
  15. 正在补充缺失文件:6.txt
  16. 随机删除一个文件:3.txt
  17. 随机删除一个文件:9.txt
  18. 随机删除一个文件:4.txt
  19. 正在补充缺失文件:1.txt
  20. 随机删除一个文件:2.txt
  21. 正在补充缺失文件:3.txt
  22. 正在补充缺失文件:4.txt
  23. 随机删除一个文件:1.txt
复制代码


####
  1. '''
  2. @ 进度条打印
  3. '''

  4. #coding=utf-8

  5. from time import sleep
  6. from multiprocessing import Process,Queue

  7. q = Queue()

  8. ico = [
  9.     ['0%',''],
  10.     ['10%','X'],
  11.     ['20%','XX'],
  12.     ['30%','XXX'],
  13.     ['40%','XXXX'],
  14.     ['50%','XXXXX'],
  15.     ['60%','XXXXXX'],
  16.     ['70%','XXXXXXX'],
  17.     ['80%','XXXXXXXX'],
  18.     ['90%','XXXXXXXXX'],
  19.     ['100%\n','XXXXXXXXXX'],
  20.     ]

  21. def run(q):
  22.     while True:
  23.         if not q.empty():
  24.             t = q.get()
  25.             if t == None: break
  26.             print('\r{:<10}{}'.format(t[1],t[0]),end='',flush=True)
  27.    
  28.    
  29. if __name__ == "__main__":
  30.     n = 0
  31.     arr = []
  32.     p = Process(target=run,args=(q,))
  33.     p.start()
  34.     print('开始计算 1 累加到 10 :')
  35.     for i in range(11):
  36.         n += i
  37.         q.put(ico[i])
  38.         sleep(0.2)
  39.     q.put(None)
  40.     p.join()
  41.     print('计算 1 累加到 10 完成:{}'.format(n))
  42.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-31 17:31:30 From FishC Mobile | 显示全部楼层
Emmmmm 应该没什么用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 18:18:44 | 显示全部楼层
_2_ 发表于 2020-7-31 17:31
Emmmmm 应该没什么用

或许,可以改一改?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-31 18:22:36 | 显示全部楼层
_2_ 发表于 2020-7-31 17:31
Emmmmm 应该没什么用

学习多进程,感觉不得劲,总是要有项目才有力量!所有就写了个这!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 08:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表