你好,我昨天的第一篇文章是关于asyncio的基本理解,现在我有另一个问题。
第一篇文章 Converting concurrent futures to Asyncio python3.7
我使用asyncio函数运行了两次此代码,一次使用纯线程运行了该代码。
import asyncio
import requests
import multiprocessing
from concurrent import futures
async def poll_data(data):
response = requests.get('https://breadcrumbscollector.tech/feed/')
print(f'Got data of length: {len(response.content)} in just {response.elapsed}')
async def start(loops):
if loops:
await loop.run_in_executor(loops)
def start_loop(data):
loops = []
loop = asyncio.new_event_loop()
for foo in data:
task = loop.create_task(poll_data(foo))
loops.append(task)
if loops:
loop.run_until_complete(asyncio.wait(loops))
def poll_data_1(data):
response = requests.get('https://breadcrumbscollector.tech/feed/')
print(f'Got data of length: {len(response.content)} in just {response.elapsed}')
data =range(10)
CPUS = multiprocessing.cpu_count()
# asyncio
#with multiprocessing.Pool(processes=CPUS, maxtasksperchild=1) as pool:
# pool.imap_unordered(start_loop(data), 1)
# pool.close()
# pool.join()
# threading
max_workers = 10
concurrent = futures.ThreadPoolExecutor(max_workers)
with concurrent as ex:
ex.map(poll_data_1, data)
结果如下:
Asyncio + Multiprocessing
$ time python3 test.py
Got data of length: 160590 in just 0:00:01.036489
Got data of length: 160590 in just 0:00:00.947815
Got data of length: 160590 in just 0:00:00.557941
Got data of length: 160590 in just 0:00:00.573095
Got data of length: 160590 in just 0:00:00.484566
Got data of length: 160590 in just 0:00:00.954783
Got data of length: 160590 in just 0:00:00.930594
Got data of length: 160590 in just 0:00:00.915454
Got data of length: 160590 in just 0:00:01.057445
Got data of length: 160590 in just 0:00:00.959989
real 0m11.576s
user 0m0.533s
sys 0m0.220s
Threading
$ time python3 test.py
Got data of length: 160590 in just 0:00:00.605912
Got data of length: 160590 in just 0:00:01.632352
Got data of length: 160590 in just 0:00:02.022035
Got data of length: 160590 in just 0:00:02.045181
Got data of length: 160590 in just 0:00:01.992517
Got data of length: 160590 in just 0:00:01.980496
Got data of length: 160590 in just 0:00:02.030631
Got data of length: 160590 in just 0:00:01.975410
Got data of length: 160590 in just 0:00:02.008469
Got data of length: 160590 in just 0:00:02.044983
real 0m4.449s
user 0m0.533s
sys 0m0.229s
我认为使用multiprocessing和asyncio的结果会更快。还是我在这里做错了什么?