"""
@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))
来源:CSDN
作者:SAGGITARXM GODESS XM
链接:https://blog.csdn.net/xuezhangjun0121/article/details/103582146