环境配置
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接口调用封装
"""
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,执行fetch,返回一个future对象。后续也都会走runner = Runner(result, future, yielded)
、future.add_done_callback(lambda _: runner)
。
上游添加一个接口服务,加大接口的超时时间,打印接收到请求和请求完成的日志,tornado中起一个接口调用上述方法,这样就能够比较方便的定位tornado发出请求的代码位置。
最终定位的位置是
tornado/curl_httpclient.py
_handle_force_timeout
ret, num_handles = self._multi.socket_all() 此处进行的非阻塞请求
网友评论