【Python】多线程和多进程常用方法

# from multiprocessing.pool import ThreadPool # 线程池:使用方法和Pool一样
from multiprocessing.dummy import Pool as ThreadPool # 两种线程池都可以
from multiprocessing import Pool # 创建进程池
import numpy as np
import time
from tqdm import tqdm
# Python官网说明:
# https://docs.python.org/zh-cn/3/library/multiprocessing.html#using-a-pool-of-workers

# 几点总结:
# 1. 进程必须要在__main__函数中才能运行,线程不用
# 2. 正常使用直接map,想看速度imap;
# 3. map结果直接用,imap结果不好拿出来,写进文件就没事了

def fun1(x):
    time.sleep(0.005)  # 假设函数的运行时间为0.005s
    return x
if __name__ == '__main__':
    a = np.arange(100)
    # res = [fun1(i) for i in a]

    ''' 多进程 '''
    # 法一
    with Pool(12) as p:
        res2 = p.map(fun1,a) 
    # 法二
    res = Pool(12).map(fun1,a)
    print(res) # [0,1,2...99]
    # 法三
    res = Pool(12).imap(fun1,a) # 顺序不变
    for i in tqdm(res):
        pass
    # 法四
    res = Pool(12).imap_unordered(fun1,a) # 顺序打乱

    ''' 多线程 '''
    with ThreadPool(12) as p:
        res = p.map(fun1,a)
    res = ThreadPool(12).map(fun1,a)
    res = ThreadPool(12).imap(fun1,a)
    for _ in tqdm(res):
        pass
    res = ThreadPool(12).imap_unordered(fun1,a)

你可能感兴趣的:(python,开发语言)