沐风2021-进程池

[复制链接]
作者: ecithy | 时间: 2021-5-30 23:20:40 | python基础|
0 55
发表于 2021-5-30 23:20:40| 显示全部楼层 |阅读模式

进程池的使用

import time
from concurrent.futures import ProcessPoolExecutor


def task(num):
    print("执行", num)
    time.sleep(2)


if __name__ == '__main__':
    # 修改模式
    pool = ProcessPoolExecutor(4)
    for i in range(10):
        pool.submit(task, i)
    print('沐风')
    print('精易')

等待进程池

等待进程池的任务执行完毕

import time
from concurrent.futures import ProcessPoolExecutor


def task(num):
    print("执行", num)
    time.sleep(2)


if __name__ == '__main__':

    pool = ProcessPoolExecutor(4)
    for i in range(10):
        pool.submit(task, i)
    # 等待进程池中的任务都执行完毕后,再继续往后执行。
    pool.shutdown(True)
    print('沐风')

进程的返回值

获取进程的返回值。

import time
from concurrent.futures import ProcessPoolExecutor
import multiprocessing


def task(num):
    print("执行", num)
    time.sleep(2)
    return num


def done(response):
    print(multiprocessing.current_process())
    time.sleep(1)
    print(response.result())


if __name__ == '__main__':

    pool = ProcessPoolExecutor(4)
    for i in range(20):
        fur = pool.submit(task, i)
        fur.add_done_callback(done)  # done的调用由主进程处理(与线程池不同)

    print(multiprocessing.current_process())
    pool.shutdown(True)

注意:如果在进程池中要使用进程锁,则需要基于Manager中的Lock和RLock来实现。

# 语法一
import time
import multiprocessing
from concurrent.futures.process import ProcessPoolExecutor


def done(response):
    print(multiprocessing.current_process())
    time.sleep(1)
    print(response.result())


def task(lock, i):
    print("开始", i)
    with lock:
        # 假设文件中保存的内容就是一个值:10
        with open('1.txt', mode='r', encoding='utf-8') as f:
            current_num = int(f.read())

        print("排队抢票了")
        time.sleep(1)
        current_num -= 1

        with open('1.txt', mode='w', encoding='utf-8') as f:
            f.write(str(current_num))
    return 'ok'


if __name__ == '__main__':
    pool = ProcessPoolExecutor(4)
    # lock_object = multiprocessing.RLock() # 不能使用
    manager = multiprocessing.Manager()
    lock_object = manager.RLock()  # Lock

    for i in range(20):
        future = pool.submit(task, lock_object, i)
        future.add_done_callback(done)  # done的调用由主进程处理(与线程池不同)
# 语法二
import time
import multiprocessing
from concurrent.futures.process import ProcessPoolExecutor


def task(lock, i):
    print("开始", i)
    with lock:
        # 假设文件中保存的内容就是一个值:10
        with open('1.txt', mode='r', encoding='utf-8') as f:
            current_num = int(f.read())

        print("排队抢票了")
        time.sleep(1)
        current_num -= 1

        with open('1.txt', mode='w', encoding='utf-8') as f:
            f.write(str(current_num))
    return 'ok'


if __name__ == '__main__':
    pool = ProcessPoolExecutor(4)
    # lock_object = multiprocessing.RLock() # 不能使用
    manager = multiprocessing.Manager()
    lock_object = manager.RLock()  # Lock

    future_list = list()
    for i in range(10):
        future = pool.submit(task, lock_object, i)
        future_list.append(future)

    pool.shutdown(True)  # 阻塞
    for fu in future_list:
        print(fu.result())
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

!jz_fbzt! 快速回复 !jz_sctz! !jz_fhlb! 按钮
快速回复 返回列表 返回顶部