線程池

https://blog.csdn.net/qq_40317897/article/details/89921083 

"""
@author: zhangjun.xue
@time: 2019/12/17 22:06
@file: gevent_work_test.py
@desc:
"""
import time
import gevent
import requests
from threading import Thread
from concurrent.futures import ThreadPoolExecutor


def gevent_work_test(work_list, func):
    """
    :param work_list:
    :return:
    """
    res = list()
    g_list = list()
    for work in work_list:
        g_list.append(gevent.spawn(func, work))
    gevent.joinall(g_list)

    for i, g in enumerate(g_list):
        res.append(g.value)

    return res


def long_time_work(work):
    s_t = time.time()
    # time.sleep(5)
    url = 'http://www.baidu.com'
    res = requests.get(url=url)
    print('res.status_code = ', res.status_code)
    print('res.text = ', res.text)
    consume_time = time.time() - s_t
    print('---------- long_time_work ------------work = {} consume_time = {}'.format(work, consume_time))
    return consume_time


# 多線程
def multi_thread_work(func, work_list):

    ths = []
    for work in work_list:
        th = Thread(target=func, args=(work))
        th.start()
        ths.append(th)
    for th in ths:
        th.join()


def thread_pool_work(func, params_list):
    # 線程池併發方式
    results = list()
    thread_pool_start_time = time.time()
    with ThreadPoolExecutor(len(params_list)) as executor:
        for params in params_list:
            results.append(executor.submit(func, params))
    results = [data.result() for data in results]
    return results


work_list = [1, 2, 3, 4, 5, 6]

multi_thread_work_list = ["1", "2", "3", "4", "5", "6"]


if __name__ == "__main__":
    pass
    # 單線程
    # res = long_time_work(7)

    # 協程
    # gevent_start_time = time.time()
    # gevent_res = gevent_work_test(work_list, long_time_work)
    # print('gevent_work consume_time = {}'.format(time.time()-gevent_start_time))

    # 多線程
    # multi_thread_start_time = time.time()
    # multi_thread_work(long_time_work, multi_thread_work_list)
    # print('multi_thread_work consume_time = {}'.format(time.time() - multi_thread_start_time))

    # 線程池
    thread_pool_work_start_time = time.time()
    res = thread_pool_work(long_time_work, multi_thread_work_list)
    print('thread_pool_work consume_time = {}'.format(time.time() - thread_pool_work_start_time))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章