python线程池ThreadPoolExecutor和进程池ProcessPoolExecutor使用

1、线程池ThreadPoolExecutor

"""
    线程池,使用单核CPU
"""
from concurrent.futures import ThreadPoolExecutor
import random
import time
import threading
from datetime import datetime
import multiprocessing
import numpy as np


def print_func(x):
    sleep_time = random.randint(1, 10)
    sleep_time = 2
    time.sleep(sleep_time)
    # 线程中进行大计算量,观察CPU使用情况,使用单核CPU
    x1 = np.random.rand(1000, 1000)
    x2 = np.random.rand(1000, 1000)
    y = np.matmul(x1, x2)
    print("current time: {}, process id: {}, thread id: {}, get: {}, sleep time: {}s".format(
        datetime.now().strftime("%Y-%m-%d %H:%M:%S"), multiprocessing.current_process().ident,
        threading.currentThread().ident,
        x, sleep_time))
    return x


print("strart time: {}".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
pool = ThreadPoolExecutor(max_workers=4)
task_list = []
for i in range(50):
    # 默认异步提交任务到线程池
    task = pool.submit(print_func, i)
    task_list.append(task)
    """
    status = task.done()
    # 同步等待线程返回结果
    while not status:
        status = task.done()
    # 线程执行完成后,done()返回为True,获取线程返回结果
    result = task.result()
    # print("result: {}".format(result))
    """
for task in task_list:
    while not task.done():
        continue
    print(task.result())
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("end time: {}".format(end_time))

2、进程池ProcessPoolExecutor

"""
    进程池,使用多核CPU
"""
from concurrent.futures import ProcessPoolExecutor
import random
import time
import threading
from datetime import datetime
import multiprocessing
import numpy as np


def print_func(x):
    sleep_time = random.randint(1, 10)
    sleep_time = 2
    time.sleep(sleep_time)
    x1 = np.random.rand(1000, 1000)
    x2 = np.random.rand(1000, 1000)
    y = np.matmul(x1, x2)

    print("current time: {}, process id: {}, thread id: {}, get: {}, sleep time: {}s".format(
        datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        multiprocessing.current_process().ident,
        threading.currentThread().ident,
        x, sleep_time))
    return x


if __name__ == '__main__':
    print("strart time: {}".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
    pool = ProcessPoolExecutor(max_workers=4)
    task_list = []
    for i in range(50):
        # 默认异步提交任务到进程池
        task = pool.submit(print_func, i)
        task_list.append(task)
        """
        status = task.done()
        # 同步等待进程返回结果
        while not status:
            status = task.done()
        # 进程执行完成后,done()返回为True,获取进程返回结果
        result = task.result()
        print("result: {}".format(result))
        """
    for task in task_list:
        while not task.done():
            continue
        print(task.result())
    end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print("end time: {}".format(end_time))

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的进程线程池是用来进行并发处理任务的工具。进程是在一个子中放入固定数量的进程,当有任务到达时,就从子中拿一个进程来处理任务,处理完毕后再放回子中等待任务。进程的数量是固定的,因此同一时间最多有固定数量的进程在运行。这样可以避免增加操作系统的调度难度,并节省开闭进程的时间,实现并发效果。 在Python中,可以使用`concurrent.futures`模块中的`ProcessPoolExecutor`类来创建进程。通过实例化`ProcessPoolExecutor`类,可以获得一个指定大小的进程。例如,可以使用以下代码实例化一个进程并指定大小为整数`n`: ``` from concurrent.futures import ProcessPoolExecutor pool = ProcessPoolExecutor(n) ``` 其中`n`是进程的大小,即进程的数量。 线程池进程的原理类似,不同之处在于线程池是在一个子中放入固定数量的线程来处理任务。线程池使用方式也类似,可以使用`concurrent.futures`模块中的`ThreadPoolExecutor`类来创建线程池,并指定大小。线程池使用进程类似,都是从子中获取线程或进程来处理任务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python--进程线程池](https://blog.csdn.net/weixin_43988680/article/details/124284555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值