并发编程基础
1.多线程 threading:
import time
import threading
def worker(n):
print('{}函数执行于:{}'.format(threading.current_thread().name, time.ctime()))
time.sleep(n)
print('{}函数结束于:{}'.format(threading.current_thread().name, time.ctime()))
def main():
print('【主函数执行于:{}】'.format(time.ctime()))
threads = [] # 列表目的:同时启动
t1 = threading.Thread(target=worker, args=(4,))
threads.append(t1)
t2 = threading.Thread(target=worker, args=(2,))
threads.append(t2)
for i in threads:
i.start() # 启动线程
for i in threads:
i.join() # 要求主线程等待
print('【主函数结束于:{}】'.format(time.ctime()))
if __name__ == '__main__':
main()
2.自定义多线程 threading:
import time
import threading
def worker(n):
print('{}函数执行于:{}'.format(threading.current_thread().name, time.ctime()))
time.sleep(n)
print('{}函数结束于:{}'.format(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('【主函数执行于:{}】'.format(time.ctime()))
threads = [] # 列表目的:同时启动
# t1 = threading.Thread(target=worker, args=(4,))
t1 = MyThread(worker, (4,))
threads.append(t1)
# t2 = threading.Thread(target=worker, args=(2,))
t2 = MyThread(worker, (2,))
threads.append(t2)
for i in threads:
i.start() # 启动线程
# i.run()
for i in threads:
i.join() # 要求主线程等待
print('【主函数结束于:{}】'.format(time.ctime()))
if __name__ == '__main__':
main()
3.同步语锁:
import threading
import time
import random
lock = threading.Lock() # 同步锁
eggs = []
def put_egg(n, lst=[]):
# lock.acquire() # 同步锁
with lock:
for i in range(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 i in threads:
i.start()
for i in threads:
i.join()
print(eggs)
if __name__ == '__main__':
main() # [1, 1, 2, 3, 4, 2, 3, 1, 4, 5, 5, 2, 3, 4, 5]
# [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
4.LIFO队列 queue
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) # 拿出数据项,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()
5.多进程multiprocessing
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()}')
process = []
p1 = multiprocessing.Process(target=func, args=(4,))
process.append(p1)
p2 = multiprocessing.Process(target=func, args=(2,))
process.append(p2)
for i in process:
i.start()
for i in process:
i.join()
print(f'主函数结束于:{time.ctime()}')
if __name__ == '__main__':
main()
多进程multiprocessing 较(多线程),充分运用多核多CPU的计算能力,适合于CPU计算密集型;多线程适合IO密集型
6.concurrent.futures
import time
import concurrent.futures
numbers = list(range(1, 11))
def count(n):
a = 0
for i in range(10000000):
a += i
return a * n
def worker(x):
reult = count(x)
print(f'数字:{x}的计算结果是:{reult}')
# 顺序执行
def sequential_exection():
start_time = time.process_time()
for i in numbers:
worker(i)
print(f'顺序执行花费时间:{time.process_time() - start_time}秒')
# 线程池执行
def threading_excution():
start_time = time.process_time()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as excutor:
for i in numbers:
excutor.submit(worker, i)
print(f'线程池执行花费时间:{time.process_time() - start_time}秒')
# 进程池执行
def process_exection():
start_time = time.process_time()
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as excutor:
for i in numbers:
excutor.submit(worker, i)
print(f'进程池执行花费时间:{time.process_time() - start_time}秒')
if __name__ == '__main__':
# sequential_exection()
# threading_excution()
process_exection()