import asyncio
import aiohttp
import time, json
from aiomultiprocess import Pool
start = time.time()
url = 'http://member-service.test.cn/member/login'
headers = {"Content-Type": "application/json;charset=UTF-8"}
async def get(phone):
session = aiohttp.ClientSession()
response = await session.post(url=url, data=json.dumps(
{"identityType": 1, "password": "111111", "platformSource": "Mall", "remember": True,
"terminalType": "PC", 'identity': phone}), headers=headers)
result = await response.text()
session.close()
print(result)
return result
async def request():
phones = ['186%08d' % x for x in range(0, 10)]
async with Pool() as pool:
result = await pool.map(get, phones) # can't work
return result
coroutine = request()
task = asyncio.ensure_future(coroutine)
loop = asyncio.get_event_loop()
loop.run_until_complete(task)
end = time.time()
print('Cost time:', end - start)
我尝试使用aiomultiprocess
创建一个池。
async with Pool() as pool:
result = await pool.map(get, phones)
但实际上不能调用get函数