import asyncio
import threading
import time
from aiohttp import ClientSession
import requests_async
# 数仓特征url
dw_pre_url ="xxx"
dw_pre_url_2 ="xxx"
dw_pre_url_3 ="xxx"# 实时特征url
tec_pre_url ="xxx"
order_no ='aaa'
contact_mob ='bbb'asyncdefget_dw_await2(order_no):asyncwith ClientSession()as session:asyncwith session.post(url=dw_pre_url_2 + order_no)as response:# a = [i for i in range(10000000)]# post 时长很短,可以异步,但是如果是费时的操作,则需要加多线程print('Hello World:%s'% time.time())
t = threading.currentThread()print('Thread id : %d'% t.ident)# await asyncio.sleep(1)returnawait response.json()asyncdefget_dw_await3(order_no):asyncwith ClientSession()as session:asyncwith session.post(url=dw_pre_url_3 + order_no)as response:# a = [i for i in range(10000000)]print('Hello World:%s'% time.time())
t = threading.currentThread()print('Thread id : %d'% t.ident)returnawait response.json()asyncdefget_dw_await4(contact_mob):asyncwith ClientSession()as session:asyncwith session.post(url=dw_pre_url_3 + contact_mob)as response:# a = [i for i in range(10000000)]print('Hello World:%s'% time.time())
t = threading.currentThread()print('Thread id : %d'% t.ident)returnawait response.json()asyncdefget_tec(orders):if orders =='':returnNone
session = aiohttp.ClientSession()
headers ={'Content-Type':'application/json'}
payload ={"order_no": orders}
response =await session.post(tec_pre_url, headers=headers, data=json.dumps(payload))
result =await response.json()await session.close()return result
defrun():
start = time.time()
task1 = asyncio.ensure_future(get_dw_await2(order_no))
task2 = asyncio.ensure_future(get_dw_await3(order_no))
task3 = asyncio.ensure_future(get_dw_await4(contact_mob))
tasks =[task1, task2, task3]
result = loop.run_until_complete(asyncio.gather(*tasks))print(result)print(len(result))print(time.time()- start)if __name__ =='__main__':
loop = asyncio.get_event_loop()
run()
2. 普通的异步
import asyncio
import time
import traceback
from abc import ABCMeta, abstractmethod
import requests
import json
import aiohttp
import threading
asyncdefget_dw_await1():
a =[i for i inrange(10000000)]
t = threading.currentThread()print('Thread id : %d'% t.ident)print('Hello World1:%s'% time.time())await asyncio.sleep(4)return a
asyncdefget_dw_await2():
a =[i for i inrange(10000000)]
t = threading.currentThread()print('Thread id : %d'% t.ident)print('Hello World2:%s'% time.time())return a
asyncdefget_dw_await3():
a =[i for i inrange(10000000)]
t = threading.currentThread()print('Thread id : %d'% t.ident)print('Hello World3:%s'% time.time())return a
defrun():
start = time.time()
task1 = asyncio.ensure_future(get_dw_await1())
task2 = asyncio.ensure_future(get_dw_await2())
task3 = asyncio.ensure_future(get_dw_await3())
tasks =[task1, task2, task3]
result = loop.run_until_complete(asyncio.gather(*tasks))print(len(result))print(time.time()- start)if __name__ =='__main__':
loop = asyncio.get_event_loop()
run()