tornado中非同步request使用
阿新 • • 發佈:2018-12-15
使用httpRequest太無腦了,太莽了,當希望使用非同步request時,首先引入AsyncHttprequest
from tornado.httpclient import AsyncHTTPClient
將介面中的方法新增上tornado的協程符
@coroutine
根據request的方式(get或者post)封裝引數
- get方法,封裝url,分為三部分, host,介面路由以及引數串,其中介面路由和引數由 ‘?’ 分割
url = u'http://{}/{}?{}'.format(host, name, query_str)
- post方法, 將引數列表封裝為dict,並用urlencode處理
args = {'item_id': task['item_id'],
'code': status,
'message': message,
}
body = urllib.urlencode(args)
傳送請求: 由於是非同步方法,使用yield asyncHTTPClient().fetch()方法傳送 引數列表:
- url:host+介面路由, 如是get方法還有引數json串
- method: =“GET”或“POST”
- body:僅限POST方法,內容為經過urlencode後的引數dict
- headers:{‘X-Requested-With’: ‘XMLHttpRequest’},非必填
- request_timeout: 超時,非必填
獲取response內容: 使用safe_json_decode以及safte_typed_from_str來解析response
ret = yield AsyncHTTPClient().fetch(task['migrate_cb_url'], method="POST", body=body)
# todo ret
result = safe_json_decode(ret.body)
code = safe_typed_from_str(result.get('code'), int)
最後對response內容進行校驗,輸出日誌raise Error
一個模板類,僅供參考
class AsyncInnerApi(IntraApi):
@classmethod
@coroutine
def call(cls, host, name, dictargs, method='GET', request_handler=None, nocheck=False, timeout=None):
name = name.lstrip('/')
query_str = cls.urlencode(dictargs)
headers = {'X-Requested-With': 'XMLHttpRequest'}
try:
with HttpCallProfile(request_handler, u'http://{}/{}?{}'.format(host, name, query_str)):
if method == 'POST':
url = u'http://{}/{}'.format(host, name)
if timeout:
res = yield AsyncHTTPClient().fetch(
url, method=method, body=query_str, headers=headers, request_timeout=timeout)
else:
res = yield AsyncHTTPClient().fetch(
url, method=method, body=query_str, headers=headers)
else:
assert method == 'GET'
url = u'http://{}/{}?{}'.format(host, name, query_str)
if timeout:
res = yield AsyncHTTPClient().fetch(
url, method=method, headers=headers, request_timeout=timeout)
else:
res = yield AsyncHTTPClient().fetch(url, method=method, headers=headers)
except HTTPException as he:
gen_log.warn(u'Request to {}/{} failed {}'.format(host, name, unicode(he)))
if he.code == 400:
raise HTTPError(400)
else:
raise BLError(u'內部呼叫錯誤')
except Exception as e:
gen_log.warn(u'Request to {}/{} failed {}'.format(host, name, unicode(e)))
raise BLError(u'內部呼叫錯誤')
if nocheck:
raise Return(res.body)
else:
data = safe_json_decode(res.body)
if data['status'] != 1:
raise BLError(u'內部呼叫錯誤')
raise Return(data['data'])