欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 两种多进程池,线程池调带结果异步返回

程序员文章站 2022-05-24 14:25:52
...
python 两种多进线程池,线程池调

import multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

def t1(n):

    # print('进程池第{0}次调用'.format(n))
return n

# 带资源回收的进程池
def m_process_pool():
    pool = multiprocessing.Pool(processes=4, maxtasksperchild=3)
    r = []
    for i in range(10):
        r.append(pool.apply_async(t1, (i,)))
    pool.close()
    pool.join()

    for i in r:
        print(i.get())

# 不带资源回收的进程池
def process_pool():

    process_pool = ProcessPoolExecutor(max_workers=10)
    r = []
    for i in range(20):
        r.append(process_pool.submit(t1, (i)))
    for i in r:
        print(i.result())


def thread_pool():

    thread_pool = ThreadPoolExecutor(max_workers=10)
    r = []
    for i in range(20):
        r.append(thread_pool.submit(t1, (i)))
    for i in r:
        print(i.result())

相关标签: pool