Learning Activities
Py之reprint:reprint的简介、安装、使用方法之详细攻略
目录
reprint 是一个适用于 Python3 的简易变量绑定与多行输出刷新的库。特性 + 简易变量绑定,内容修改时自动刷新命令行输出 + 多行输出刷新,实现不同行内容由独立变量控制,修改特定变量即能刷新命令行中特定行的内容 + 多线程安全,使用了 threading.Lock 实现线程安全 + 无外部库依赖。
py pi地址:https://pypi.org/project/reprint/
pip install reprint
1、官方示例
- import time
- import random
- import threading
-
- from reprint import output
-
-
- def some_op(index=0):
- LENGTH = 100
- global output_list
- now = 0
- while now < LENGTH:
- max_step = LENGTH-now
- step = random.randint(1, max_step or 1)
- now += step
-
- output_list[index] = "{}{}{padding}{ending}".format(
- "-" * now,
- index,
- padding=" " * (LENGTH-now-1) if now < LENGTH else "",
- ending="|" if now < LENGTH else ""
- )
-
- time.sleep(1)
-
- output_list.append("{} finished".format(index))
-
- with output(output_type="list", initial_len=5, interval=0) as output_list:
- pool = []
- for i in range(5):
- t = threading.Thread(target=some_op, args=(i,))
- t.start()
- pool.append(t)
- [t.join() for t in pool]
评论