概述
多线程
多线程实例
import time
import _thread
def worker(n):
print(f'函数执行开始于:{time.ctime()}')
time.sleep(n)
print(f'函数执行结束于:{time.ctime()}')
def main():
print(f'主函数运行开始于:{time.ctime()}')
_thread.start_new_thread(worker, (4,))
_thread.start_new_thread(worker, (2,))
#_thread弊端
time.sleep(4)
print(f'主函数执行结束于:{time.ctime()}')
if __name__ == '__main__':
main()
_thread 模块
特点
没有控制进程结束机制
只有一个同步原语(锁)
功能少于 threading模块
.start_new_thread(function, args, **kwargs=None) 开始线程
import time
import threading
def worker(n):
print(f'{threading.current_thread().name}函数执行开始于:{time.ctime()}')
time.sleep(n)
print(f'{threading.current_thread().name}函数执行结束于:{time.ctime()}')
def main():
print(f'主函数运行开始于:{time.ctime()}')
threads = []
t1 = threading.Thread(target=worker, args=(4,))
threads.append(t1)
t2 = threading.Thread(target=worker, args=(2,))
threads.append(t2)
for t in threads:
t.start()
# 主线程等待子线程完成
for t in threads:
t.join()
print(f'主函数执行结束于:{time.ctime()}')
if __name__ == '__main__':
main()
threading 模块
.Thread 线程类
构造
.Thread(target=目标函数, args=(参数,))
自定义Thread派生类,重写run方法逻辑
.start() 启动线程
.join() 要求主线程等待
.name() 线程名称
.current_thread() 获取当前线程
import time
import threading
def worker(n):
print(f'{threading.current_thread().name}函数执行开始于:{time.ctime()}')
time.sleep(n)
print(f'{threading.current_thread().name}函数执行结束于:{time.ctime()}')
class MyThread(threading.Thread):
def __init__(self, func, args):
threading.Thread.__init__(self)
self.func = func
self.args = args
def run(self):
self.func(*self.args)
def main():
print(f'主函数运行开始于:{time.ctime()}')
threads = []
t1 = MyThread(worker, args=(4,))
threads.append(t1)
t2 = MyThread(worker, args=(2,))
threads.append(t2)
for t in threads:
t.start()
# 主线程等待子线程完成
for t in threads:
t.join()
print(f'主函数执行结束于:{time.ctime()}')
if __name__ == '__main__':
main()
threading.Lock 同步原语:锁
.acquire() 获得
.release() 释放
支持上下文操作 with lock:
import threading
import time
import random
eggs = []
lock = threading.Lock()
def put_egg(n, lst):
#锁
#lock.acquire()
with lock:
for i in rang(1, n+1):
#模拟速度
time.sleep(random.randint(0, 2))
lst.append(i)
#解锁
#lock.release()
def main():
threads = []
for i in range(3):
t = threading.Thread(target=put_egg, args=(5, eggs))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print(eggs)
if __name__ == '__main__':
main()
队列
queue 模块
Queue FIFO
Queue(maxsize=0) 构造实例
.put(item, block=True, timeout=None) 放入数据项
.get(block=True, timeout=None) 获取数据项
.task_done() 声明当前队列任务处理完毕
.join() 队列所有项处理完毕前阻塞
import threading
import queue
import time
import random
# 生产者
def producer(data_queue):
for i in range(5):
time.sleep(0.5)
item = random.randint(1, 100)
data_queue.put(item)
print(f'{threading.current_thread().name}在对类中放入数据项:{item}')
# 消费者
def consumer(data_queue):
while True:
try:
item = data_queue.get(timeout=3)
print(f'{threading.current_thread().name}从队列中移除了{item}')
except queue.Empty:
break
else:
data_queue.task_done()
def main():
q = queue.Queue()
threads = []
# 单个生产者生产
p = threading.Thread(target=producer, args=(q,))
p.start()
for i in range(2):
c = threading.Thread(target=consumer, args=(q,))
threads.append(c)
for t in threads:
t.start()
# 主线程等待完成
for t in threads:
t.join()
q.join()
if __name__ == '__main__':
main()
LifoQueue LIFO
PriorityQUeue 优先队列
multiprocessing 模块
特点
- 充分运用多核、多CPU的计算能力,适用于计算密集型任务
import time
import multiprocessing
def func(n):
print(f'{multiprocessing.current_process().name}执行开始于:{time.ctime()}')
time.sleep(n)
print(f'{multiprocessing.current_process().name}执行结束于:{time.ctime()}')
def main():
print(f'主函数运行于:{time.ctime()}')
p1 = multiprocessing.Process(target=func, args(4,))
processes.append(p1)
p2 = multiprocessing.Process(target=func, args(2,))
processes.append(p2)
for p in processes:
p.start()
for p in processes:
p.join()
print(f'主函数结束于:{time.ctime()}')
if __name__ == '__main__':
main()
concurrent.futures 模块
- ThreadRoolExcutor
- ProcessPoolExcutor
import time
import concurrent.futures
numbers = list(range(1, 11))
def count(n):
for i in range(10000000):
i += i
return i * n
def worker(x):
result = count(x)
print(f'数字:{x}的计算结果是: {result}')
# 顺序执行
def sequential_execution():
start_time = time.perf_counter()
for i in numbers:
worker(i)
print(f'顺序执行花费时间:{time.perf_counter() - start_time} 秒')
# 线程池执行
def threading_execution():
start_time = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
for i in numbers:
executor.submit(worker, i)
print(f'线程池执行花费时间:{time.perf_counter() - start_time} 秒')
# 进程池执行
def process_execution():
start_time = time.perf_counter()
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
for i in numbers:
executor.submit(worker, i)
print(f'进程池执行花费时间:{time.perf_counter() - start_time} 秒')
if __name__ == '__main__':
#sequential_execution()
#threading_execution()
process_execution()