一、线程池
from multiprocessing.pool import ThreadPool
import queue
def fake_download(q: queue.Queue):
while not q.empty():
print(q.get())
q.task_done() # 任务完成
if __name__ == '__main__':
q = queue.Queue()
for i in range(1, 5):
q.put(f'https://so.gushiwen.org/mingjus/default.aspx?page={i}&tstr=%E6%98%A5%E5%A4%A9')
pool = ThreadPool(2) # 使用2个线程
pool.apply_async(fake_download, args=(q,)) # pool中的多个线程异步地调用指定函数
q.join() # 等待所有任务完成再结束主进程
print("finished!")
二、进程池
- apply 和 apply_async 一次执行一个任务,但 apply_async 可以异步执行
- map 和 map_async 可以批量调用函数,一次执行多个任务
apply和apply_async
- apply方法是阻塞的
- apply_async是异步非阻塞的 == 异步调用函数
import time
from multiprocessing import Pool
def work(x, y, z=0):
print('~~~~~~~~~~')
time.sleep(3)
return x + y + z
if __name__ == '__main__':
pool = Pool(2)
result = pool.apply(work, (2, 3), {'z': 1}) # 阻塞主程序(主进程) == 同步
print(result)
# pool.apply_async(work,(3,4)) #不阻塞主程序 == 异步
print("finished!")
map和map_async
- pool.map 把可迭代对象映射到函数
- map 和 map_async 入参为迭代器类型,可以批量调用函数,而apply和apply_async只能一个个调用
import time
from multiprocessing import Pool
def squre(x):
print("squre程序运行中")
return x * x
if __name__ == '__main__':
start_time = time.time()
pool = Pool(2)
result = pool.map(squre, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # map会阻塞主程序
print(result)
pool.close()
end_time = time.time()
print("总耗时:{}".format(end_time - start_time))
- map_async().get() == map
- 使用map_async()必须用.get()方法获取结果集
import time
from multiprocessing import Pool
def squre(x):
print("squre程序运行中")
return x * x
if __name__ == '__main__':
start_time = time.time()
pool = Pool(2)
result = pool.map_async(squre, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) # map会阻塞主程序
print(result.get()) # get() 用来获得map映射后的结果集 [1, 4, 9, 16, 25, 36, 49, 64],结果不出来,不往下运行 (即阻塞)
pool.close()
end_time = time.time()
print("总耗时:{}".format(end_time - start_time))