環(huán)境配置
win10
pycharm64 5.0.3
python 3.6.7
tornado 5.1.1
代碼塊
@staticmethod
@gen.coroutine
def send_req(req_obj, url="", params={}, body=None, method="GET", headers=None,
connect_timeout=None, request_timeout=None, max_clients=50):
"""
異步HTTP接口調(diào)用封裝
"""
url = url_concat(url, params)
try:
request = HTTPRequest(url, method=method, body=body, headers=None,
connect_timeout=connect_timeout, request_timeout=request_timeout, validate_cert=False)
except:
req_obj.app_exception("get HTTPRequest exception")
return None
try:
response = yield AsyncHTTPClient(max_clients=max_clients).fetch(request) # HTTPResponse
except:
# req_obj.app_exception("AsyncHTTPClient exception")
logging.exception("AsyncHTTPClient exception")
return None
在yield AsyncHTTPClient(max_clients=max_clients).fetch(request)中會實例化AsyncHTTPClient令蛉,執(zhí)行fetch,返回一個future對象十电。后續(xù)也都會走runner = Runner(result, future, yielded)
内边、future.add_done_callback(lambda _: runner)
纯蛾。
上游添加一個接口服務(wù)节猿,加大接口的超時時間棠耕,打印接收到請求和請求完成的日志睛竣,tornado中起一個接口調(diào)用上述方法晰房,這樣就能夠比較方便的定位tornado發(fā)出請求的代碼位置。
最終定位的位置是
tornado/curl_httpclient.py
_handle_force_timeout
ret, num_handles = self._multi.socket_all() 此處進行的非阻塞請求