asyncio 有无ThreadPoolExecutor配合实例

task 是协程和 Future 的桥梁。

1.协程和线程池

结果中会认为配合线程池,不那么快。
但是,配合还是会带来一些好处,
比如:使用多线程,以至于整个主线程不阻塞

# coding=utf-8
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
import time

logging.basicConfig(
    level=logging.INFO,
    format=" %(asctime)s [*] %(threadName)s %(message)s"
)


def call_back01 (x):
    time.sleep(3)
    logging.info(f"Get:{x}")


async def call_back02 (x):
    await asyncio.sleep(3)
    logging.info(f"Get:{x}")


if __name__ == "__main__":
    now = time.time()
    loop = asyncio.get_event_loop()
    
    # 1.协程和线程池-----------------------------------------------------
    ex = ThreadPoolExecutor(max_workers=10)
    tasks = list()
    for i in range(20):
        tasks.append(loop.run_in_executor(ex, call_back01, i))
    
    # 2.单单协程---------------------------------------------------------
    # tasks = [asyncio.ensure_future(call_back02(i)) for i in range(20)]
    loop.run_until_complete(asyncio.wait(tasks))
    print("总用时", time.time() - now)

输出:

 2019-10-09 21:23:52,313 [*] ThreadPoolExecutor-0_1 Get:1
 2019-10-09 21:23:52,314 [*] ThreadPoolExecutor-0_0 Get:0
 2019-10-09 21:23:52,314 [*] ThreadPoolExecutor-0_2 Get:2
 2019-10-09 21:23:52,314 [*] ThreadPoolExecutor-0_3 Get:3
 2019-10-09 21:23:52,314 [*] ThreadPoolExecutor-0_5 Get:5
 2019-10-09 21:23:52,314 [*] ThreadPoolExecutor-0_4 Get:4
 2019-10-09 21:23:52,315 [*] ThreadPoolExecutor-0_7 Get:7
 2019-10-09 21:23:52,315 [*] ThreadPoolExecutor-0_6 Get:6
 2019-10-09 21:23:52,315 [*] ThreadPoolExecutor-0_9 Get:9
 2019-10-09 21:23:52,315 [*] ThreadPoolExecutor-0_8 Get:8
 2019-10-09 21:23:55,315 [*] ThreadPoolExecutor-0_1 Get:10
 2019-10-09 21:23:55,316 [*] ThreadPoolExecutor-0_3 Get:12
 2019-10-09 21:23:55,317 [*] ThreadPoolExecutor-0_2 Get:13
 2019-10-09 21:23:55,318 [*] ThreadPoolExecutor-0_0 Get:11
 2019-10-09 21:23:55,318 [*] ThreadPoolExecutor-0_5 Get:14
 2019-10-09 21:23:55,319 [*] ThreadPoolExecutor-0_4 Get:15
 2019-10-09 21:23:55,319 [*] ThreadPoolExecutor-0_7 Get:16
 2019-10-09 21:23:55,324 [*] ThreadPoolExecutor-0_8 Get:19
 2019-10-09 21:23:55,324 [*] ThreadPoolExecutor-0_9 Get:18
 2019-10-09 21:23:55,324 [*] ThreadPoolExecutor-0_6 Get:17
总用时 6.01738715171814

2.单单协程

# coding=utf-8
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
import time

logging.basicConfig(
    level=logging.INFO,
    format=" %(asctime)s [*] %(threadName)s %(message)s"
)


def call_back01 (x):
    time.sleep(3)
    logging.info(f"Get:{x}")


async def call_back02 (x):
    await asyncio.sleep(3)
    logging.info(f"Get:{x}")


if __name__ == "__main__":
    now = time.time()
    loop = asyncio.get_event_loop()
    
    # 1.协程和线程池-----------------------------------------------------
    # ex = ThreadPoolExecutor(max_workers=10)
    # tasks = list()
    # for i in range(20):
    #     tasks.append(loop.run_in_executor(ex, call_back01, i))
    
    # 2.单单协程---------------------------------------------------------
    tasks = [asyncio.ensure_future(call_back02(i)) for i in range(20)]
    loop.run_until_complete(asyncio.wait(tasks))
    print("总用时", time.time() - now)

输出:

 2019-10-09 21:23:24,115 [*] MainThread Get:0
 2019-10-09 21:23:24,116 [*] MainThread Get:2
 2019-10-09 21:23:24,116 [*] MainThread Get:6
 2019-10-09 21:23:24,116 [*] MainThread Get:14
 2019-10-09 21:23:24,117 [*] MainThread Get:19
 2019-10-09 21:23:24,117 [*] MainThread Get:18
 2019-10-09 21:23:24,117 [*] MainThread Get:17
 2019-10-09 21:23:24,118 [*] MainThread Get:16
 2019-10-09 21:23:24,118 [*] MainThread Get:13
 2019-10-09 21:23:24,118 [*] MainThread Get:15
 2019-10-09 21:23:24,118 [*] MainThread Get:12
 2019-10-09 21:23:24,119 [*] MainThread Get:11
 2019-10-09 21:23:24,119 [*] MainThread Get:10
 2019-10-09 21:23:24,119 [*] MainThread Get:9
 2019-10-09 21:23:24,119 [*] MainThread Get:8
 2019-10-09 21:23:24,119 [*] MainThread Get:5
 2019-10-09 21:23:24,120 [*] MainThread Get:7
 2019-10-09 21:23:24,120 [*] MainThread Get:4
 2019-10-09 21:23:24,120 [*] MainThread Get:1
 2019-10-09 21:23:24,120 [*] MainThread Get:3
总用时 3.0080056190490723

你可能感兴趣的:(Python,asyncio)