线程池submit和map的应用

线程池submit的应用

import time
def timeit(f):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        res = f(*args, **kwargs)
        end_time = time.time()
        print("%s函数运行时间:%.2f" % (f.__name__, end_time - start_time))
        return res

    return wrapper


# python3.2版本之后才有的;
import threading
from concurrent.futures import ThreadPoolExecutor, wait
from urllib.request import urlopen


def get_area(ip):
    url = "http://ip-api.com/json/%s" % (ip)
    urlObj = urlopen(url)

    # 服务端返回的页面信息, 此处为字符串类型
    pageContent = urlObj.read().decode('utf-8')

    # 2. 处理Json数据
    import json
    # 解码: 将json数据格式解码为python可以识别的对象;
    dict_data = json.loads(pageContent)

    print("""
                        %s
    所在城市: %s
    所在国家: %s

    """ % (ip, dict_data['city'], dict_data['country']))


@timeit
def use_ten_thread():
    #  1. 实例化线城池对象,线城池里面包含5个线程执行任务;
    pool = ThreadPoolExecutor(max_workers=10)

    futures = []
    for i in range(30):
        print("当前线程数:", threading.activeCount())
        ip = '12.13.14.%s' %(i+1)
        # 往线程池里面扔需要执行的任务, 返回的是一个对象(_base.Future()),
        f1 = pool.submit(get_area, ip)
        futures.append(f1)

    # 等待futures里面所有的子线程执行结束, 再执行主线程(join())
    wait(futures)



@timeit
def use_hundred_thread():
    #  1. 实例化线城池对象,线城池里面包含5个线程执行任务;
    pool = ThreadPoolExecutor(max_workers=100)

    futures = []
    for i in range(30):
        print("当前线程数:", threading.activeCount())
        ip = '12.13.14.%s' %(i+1)
        # 往线程池里面扔需要执行的任务, 返回的是一个对象(_base.Future()),
        f1 = pool.submit(get_area, ip)
        futures.append(f1)

    wait(futures)

if __name__ == '__main__':
    use_ten_thread()
    use_hundred_thread()

线程池map的应用
1,将数组中的每个元素提取出来当作函数的参数,创建一个个进程,放进进程池中
2,第一个参数是函数,第二个参数是一个迭代器,将迭代器中的数字作为参数依次传入函数中

示例:

import time


def timeit(f):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        res = f(*args, **kwargs)
        end_time = time.time()
        print("%s函数运行时间:%.2f" % (f.__name__, end_time - start_time))
        return res

    return wrapper


# python3.2版本之后才有的;
import threading
from concurrent.futures import ThreadPoolExecutor, wait
from urllib.request import urlopen


def get_area(ip):
    url = "http://ip-api.com/json/%s" % (ip)
    urlObj = urlopen(url)

    # 服务端返回的页面信息, 此处为字符串类型
    pageContent = urlObj.read().decode('utf-8')

    # 2. 处理Json数据
    import json
    # 解码: 将json数据格式解码为python可以识别的对象;
    dict_data = json.loads(pageContent)

    print("""
                        %s
    所在城市: %s
    所在国家: %s

    """ % (ip, dict_data['city'], dict_data['country']))


@timeit
def use_ten_thread():
    #  1. 实例化线城池对象,线城池里面包含5个线程执行任务;
    pool = ThreadPoolExecutor(max_workers=10)

    # futures = []
    # for i in range(30):
    #     print("当前线程数:", threading.activeCount())
    #     ip = '12.13.14.%s' %(i+1)
    #     # 往线程池里面扔需要执行的任务, 返回的是一个对象(_base.Future()),
    #     f1 = pool.submit(get_area, ip)
    #     futures.append(f1)
    #
    # # 等待futures里面所有的子线程执行结束, 再执行主线程(join())
    # wait(futures)

    ips = ['12.13.14.%s' % (ip + 1) for ip in range(30)]
    pool.map(get_area, ips)


@timeit
def use_hundred_thread():
    #  1. 实例化线城池对象,线城池里面包含5个线程执行任务;
    pool = ThreadPoolExecutor(max_workers=100)

    # futures = []
    # for i in range(30):
    #     print("当前线程数:", threading.activeCount())
    #     ip = '12.13.14.%s' % (i + 1)
    #     # 往线程池里面扔需要执行的任务, 返回的是一个对象(_base.Future()),
    #     f1 = pool.submit(get_area, ip)
    #     futures.append(f1)
    #
    # wait(futures)

    ips = ['12.13.14.%s' % (ip + 1) for ip in range(30)]
    pool.map(get_area, ips)



if __name__ == '__main__':
    use_ten_thread()
    use_hundred_thread()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值