07进程池线程池

提交任务的方式:
同步调用:提交任务后,就在原地等待,等待任务执行完毕,拿到任务的返回值,才能继续执行下一行代码,导致程序串行
异步调用+回调机制:提交任务后,不在原地等待,任务一旦执行完毕就会触发回调函数的执行,程序是并发执行

进程的执行调用:
阻塞:
非阻塞:

进程池:
同步调用实例:result()取得结果,导致同步调用
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import os, time, random

def task(n):
    print('{} is running'.format(os.getpid()))
    time.sleep(random.randint(1,3))
    return n**2

def handle(res):
    print('handle res', res)

if __name__ == '__main__':
    pool = ProcessPoolExecutor(4)
    for i in range(5):
        #result(),取得结果,所以此时是同步调用,要等待res的结果
        res = pool.submit(task,i).result()
        # print(res)
        handle(res)

    pool.shutdown(wait=True)#wait=True,等待池内所有任务执行完毕回收完资源后才继续
    print('')
异步调用实例,不用result取得结果,回调机制处理结果
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import os, time, random

def task(n):
    print('{} is running'.format(os.getpid()))
    time.sleep(random.randint(1,3))
    # res = n**2
    # handle(res)
    return n**2

def handle(res):
    res = res.result()
    print('handle res', res)

if __name__ == '__main__':
    pool = ProcessPoolExecutor(2)
    for i in range(5):
        # pool.submit(task,i).add_done_callback(handle)#添加回调函数,等于下面两句
        obj = pool.submit(task,i)
        obj.add_done_callback(handle)#handle(obj)

    pool.shutdown(wait=True)#wait=True,等待池内所有任务执行完毕回收完资源后才继续
    print('')

 

线程池
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread
import requests
import time


def get_page(url):
    print('{} get {}'.format(current_thread().getName(), url))
    time.sleep(2)
    response = requests.get(url)
    if response.status_code == 200:
        return {'url':url, 'text':response.text}

def parse_page(res):
    res = res.result()
    print('parse:{},text:{}'.format(res['url'],len(res['text'])))

if __name__ == '__main__':
        urls = [
            'https://www.baidu.com',
            'https://www.python.org',
            'https://i-beta.cnblogs.com/',
            'https://www.cnblogs.com/xiaoyuanqujing/p/11636160.html',
            'https://www.cnblogs.com/linhaifeng/p/7278389.html'
        ]
        pools = ThreadPoolExecutor(2)
        for url in urls:
            pools.submit(get_page, url).add_done_callback(parse_page)

 


 

 
posted @ 2021-03-05 16:38  cheng4632  阅读(60)  评论(0)    收藏  举报