第十章:使用进程、线程和协程提供并发性-asyncio:异步I/O、事件循环和并发工具-结合使用协程、线程和进程-进程

10.5.14.2 进程
ProcecssPoolExecutor的工作是类似的,但是会创建一组工作进程而不是工作线程。尽管使用单独的进程需要更多系统资源,但对于计算强度很大的操作,在各个CPU上运行一个单独的任务会很有意义。

# Changes from asyncio.executor_thread.py

import asyncio
import concurrent.futures
import logging
import sys
import time


def blocks(n):
    log = logging.getLogger('blocks({})'.format(n))
    log.info('running')
    time.sleep(0.1)
    log.info('done')
    return n ** 2


async def run_blocking_tasks(executor):
    log = logging.getLogger('run_blocking_tasks')
    log.info('starting')

    log.info('creating executor tasks')
    loop = asyncio.get_event_loop()
    blocking_tasks = [
        loop.run_in_executor(executor,blocks,i)
        for i in range(6)
        ]
    log.info('waiting for executor tasks')
    completed,pending = await asyncio.wait(blocking_tasks)
    results = [t.result() for t in completed]
    log.info('result:{!r}'.format(results))

    log.info('exiting')


if __name__ == '__main__':
    # Configure logging to show the ID of the process
    # where the lig message originates.
    logging.basicConfig(
        level=logging.INFO,
        format='PID %(process)5s %(name)18s: %(message)s',
        stream=sys.stderr,
        )

    # Create a limited process pool.
    executor = concurrent.futures.ProcessPoolExecutor(
        max_workers=3,
        )

    event_loop = asyncio.get_event_loop()
    try:
        event_loop.run_until_complete(
            run_blocking_tasks(executor)
            )
    finally:
        event_loop.close()

从线程转向进程需要做的唯一的改变是要创建一种不同类型的执行器。这个例子还修改了日志格式串,使它包含进程ID而不是线程名,以展示任务在单独的进程中运行。
运行结果:
在这里插入图片描述

你可能感兴趣的:(Python标准库)