python使用多线程来执行函数
2023-09-11 14:15:15 时间
使用多线程来高效执行程序!
示例代码1:
generate_data.py
import random
for i in range(100):
a = random.randint(10, 99)
b = random.randint(10, 99)
data = f'第{i+1}条数据\n'
with open('text.txt', 'a', encoding='utf-8') as f:
f.write(data)
main.py
import threading
import time
from queue import Queue
# 定义消费者
class Consumer(threading.Thread):
def run(self) -> None:
global queue
while queue.qsize() > 0:
msg = self.name + "消费了" + queue.get().replace('\n', '')
print(msg)
time.sleep(1)
def read_file():
file_data = open('text.txt', 'r', encoding='utf-8')
global queue
for line in file_data:
queue.put(line)
file_data.close()
if __name__ == '__main__':
queue = Queue()
read_file()
consumer_lst = []
for _ in range(5):
consumer = Consumer()
consumer.start()
consumer_lst.append(consumer)
for consumer in consumer_lst:
consumer.join()
print("程序执行完毕!!!")
运行结果:
在上面输出的结果过,使用print()打印的数据比较混乱,比较影响观看效果。这时对混乱输出可以做一定的处理。
python多线程时并发并非并行,线程之间并非严格遵守顺序,这就会造成线程不安全的情况,例如print是自动添加换行的,在换行时可能线程不安全,导致换行和下一句输出发生混乱。
解决方案:
- 手动输入换行符,例如print(url+'\n',end=''),以空字符结尾,在输出内容后面主动加入换行符,这样就不会存在错位的问题。
- 对 print() 加锁
示例代码2:
import threading
import time
from queue import Queue
mutex = threading.RLock()
# 定义消费者
class Consumer(threading.Thread):
# 定义多线程有序输出
def order_output(self, data):
print("+" * 10)
print(data)
print("*" * 10)
def run(self) -> None:
global queue
while queue.qsize() > 0:
msg = self.name + "消费了" + queue.get().replace('\n', '')
# print(msg)
# 方法一:手动输入换行符
# print(msg + '\n', end='')
# 方法二:使用锁
with mutex:
self.order_output(msg)
time.sleep(1)
def read_file():
file_data = open('text.txt', 'r', encoding='utf-8')
global queue
for line in file_data:
queue.put(line)
file_data.close()
if __name__ == '__main__':
queue = Queue()
read_file()
consumer_lst = []
for _ in range(5):
consumer = Consumer()
consumer.start()
consumer_lst.append(consumer)
for consumer in consumer_lst:
consumer.join()
print("程序执行完毕!!!")
运行结果:
有时候想知道程序执行的速度,我们可以添加进度条来显示出来。可以借助tqdm库来实现。
示例代码3:
import threading
import time
from queue import Queue
from tqdm import tqdm
mutex = threading.RLock()
# 定义消费者
class Consumer(threading.Thread):
# 定义多线程有序输出
def order_output(self, data):
print("+" * 10)
print(data)
print("*" * 10)
def run(self) -> None:
global queue
while queue.qsize() > 0:
pbar.update(1)
msg = self.name + "消费了" + queue.get().replace('\n', '')
# print(msg)
# 方法一:手动输入换行符
print(msg + '\n', end='')
# 方法二:使用锁
# with mutex:
# self.order_output(msg)
time.sleep(5)
def read_file():
file_data = open('text.txt', 'r', encoding='utf-8')
global queue
total = 0
for line in file_data:
queue.put(line)
total += 1
file_data.close()
return total
if __name__ == '__main__':
queue = Queue()
total = read_file()
# 使用tqdm进度条
pbar = tqdm(total=total, desc='Consumer')
pbar.set_postfix(aa=total, bb=total - 1)
consumer_lst = []
for _ in range(5):
consumer = Consumer()
consumer.start()
consumer_lst.append(consumer)
for consumer in consumer_lst:
consumer.join()
print("程序执行完毕!!!")
运行结果:
相关文章
- Python中python-nmap模块的使用
- Python中python-nmap模块的使用
- 将numpy和python类型数据的相互转换
- python 保存图片_用Python从视频中提取每一帧的图片
- python最优效率的编程方式
- Python实现多线程并发下载大文件(断点续传支持)
- 2023版python安装教程奉上,Python永久使用 超详细版,一看就会【小白友好】
- 每天一个python小知识——如何在Python 3中转换数据类型
- 119 python高级 - 多线程threading(二)
- 26 python - 引用
- python 多线程
- python threading 模块多线程详解
- 多少测试人都不知道... 一行 Python 代码竟 然能实现并行
- python字符串前面加上'r'的作用
- python: 多线程实现的两种方式及让多条命令并发执行
- 大数据图书分享-Python数据可视化实战课程
- Python 相对路径和绝对路径--python实战(九)
- Python 网络编程之过多线程在两个单独的 GUI 之间进行通信(教程含完整源码)
- Python基础入门-字典
- Python学习---重点模块之shelve
- Python 常见问题 之 python 安装包下载安装速度慢 的 快速解决方法(之一)
- Python 基础 之 Ubuntu 上安装 python 和 python-pip
- 2.1 The Python Interpreter(python解释器)
- [Python]2分钟完成python + Selenium Web端自动化环境搭建,开启~~~
- Platt SMO 支持向量机算法(Python实现)