Python3.7下测试单线程,多线程和多协成

测试单线程,多线程和多协成

Posted by Carlos on August 20, 2018

测试代码如下:

from concurrent import futures
import grequests
import requests
import time


nowtime=time.time()
url="http://www.baidu.com"
# 次数
count = 100
tasks=(grequests.get(url) for _ in range(count))

def exception_handler(request, exception):
    print("request failed:", exception)

gr=grequests.imap(tasks, exception_handler=exception_handler)

print([len(i.text) for i in gr],time.time()-nowtime)

nowtime=time.time()
print([len(requests.get(url).text) for _ in range(count)], time.time()-nowtime)

nowtime=time.time()
def func(url):
	return len(requests.get(url).text)

with futures.ThreadPoolExecutor(50) as executor:
	res = executor.map(func, [url] * count)

print(list(res), time.time()-nowtime)

nowtime=time.time()
def func_test(urls):
	for url in urls:
		yield len(requests.get(url).text)

print([item for item in func_test([url] * count)], time.time()-nowtime)

结果如下:

[2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381] 3.82793927192688
[2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381] 11.528904438018799
[2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381] 0.6845905780792236
[2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381, 2381] 8.500478029251099

总结:多线程效果最优