python线程池和进程池应用

一、多线程

import random
import sys
import time
import threading


def func(count):
    time_out = random.randint(2, 5)
    sys.stdout.write(f'count={count}, 线程名称是: {threading.current_thread().name}, 此函数线程运行{time_out}秒\n')
    time.sleep(time_out)    # 强制等待2-5秒
    return count


class MyThread(threading.Thread):
    def __init__(self, args):
        super(MyThread, self).__init__()
        self.args = args

    def run(self):
        func(self.args)


if __name__ == '__main__':
    start_time = time.time()
    task = []
    for i in range(10):
        # ---------------1、直接调用---------------
        # t = threading.Thread(target=func, args=(i,))  # args函数对应的参数,是元组
        # ---------------2、继承threading.Thread类重写run()方法。-----------------
        t = MyThread(i)
        t.start()
        task.append(t)
    for t in task:
        t.join()
    print(f'耗时: {time.time() - start_time}, 线程名称是: {threading.current_thread().name}')

二、线程池

import random
import sys
import time
import threading
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed


def func(count):
    time_out = random.randint(2, 5)
    sys.stdout.write(f'count={count}, 线程名称是: {threading.current_thread().name}, 此函数线程运行{time_out}秒\n')
    time.sleep(time_out)    # 强制等待2-5秒
    return count


if __name__ == '__main__':
    start_time = time.time()
    pool = ThreadPoolExecutor(10)

    # ---------------------------1、参数确定的情况下,直接加任务-----------------------------------
    # task = pool.map(func, range(10))
    # for i in task:
    #     data = i
    #     print(f'data={data}, 当前线程名称是: {threading.current_thread().name}\n')

    # ---------------------------2、逐步遍历添加任务--------------------------------------------
    task = []
    for i in range(10):
        task.append(pool.submit(func, i))
    # 等待线程池中任务执行完后再执行主线程
    # for i in task:
    for i in as_completed(task):
        # as_completed()先执行完先返回,直接task就是按加入顺序返回
        data = i.result()
        print(f'data={data}, 当前线程名称是: {threading.current_thread().name}\n')
    # --------------------------------------------------------------------------------------
    
    print(f'时间间隔是: {time.time() - start_time}, 线程名称是: {threading.current_thread().name}')

三、多进程(和多线程一毛一样)

import os
import random
import sys
import time
from multiprocessing import Process


def func(count):
    time_out = random.randint(2, 5)
    sys.stdout.write(f'count={count}, 进程名称是: 进程-{os.getpid()}, 此函数线程运行{time_out}秒\n')
    time.sleep(time_out)  # 强制等待2-5秒
    return count


class MyProcess(Process):
    def __init__(self, arg):
        super(MyProcess, self).__init__()
        self.arg = arg

    def run(self):
        func(self.arg)


if __name__ == '__main__':
    start_time = time.time()
    task = []
    for i in range(10):
        # p = Process(target=func, args=(i,))
        p = MyProcess(i)
        p.start()
        task.append(p)
    for p in task:
        p.join()
    print(f'耗时: {time.time() - start_time}, 进程名称是: 进程-{os.getpid()}')

四、进程池

import os
import random
import sys
import time
from multiprocessing.pool import Pool


def func(count):
    time_out = random.randint(2, 5)
    sys.stdout.write(f'count={count}, 进程名称是: 进程-{os.getpid()}, 此函数线程运行{time_out}秒\n')
    time.sleep(time_out)  # 强制等待2-5秒
    return count


if __name__ == '__main__':
    start_time = time.time()
    p = Pool(10)  # Pool的默认大小是你所用的电脑CPU的核数,CPU核数可通过os.cpu_count()获得
    # ---------------------1、参数确定的情况下,直接加任务---------------------------
    # task = p.map(func, range(10))
    # ----------------------2、逐步遍历添加任务--------------------------
    task = []
    for i in range(10):
        task.append(p.apply_async(func, args=(i,)))  # apply_async 异步
    p.close()  # 在调用p.join()之前需要先调用p.close()
    p.join()
    # ------------------------------------------------
    for t in task:
        data = t.get()  # 同步就不需要.get()了
        print(f'data={data}, 当前线程名称是: 进程-{os.getpid()}')
    print(f'耗时: {time.time() - start_time}, 进程名称是: 进程-{os.getpid()}')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值