我使用httpclient.HTTPRequest库发送异步请求,但需要在请求之间添加延迟。 这意味着我可以说我配置RPS(每秒请求数)= 5.然后我每隔0.2发送一个请求但是异步。如何在不等待每个请求响应的情况下异步发送请求。
这是我的代码:
def process_campaign(self, campaign_instance):
ioloop.IOLoop.current().run_sync(lambda: start_campaign(campaign_instance))
@gen.coroutine
def start_campaign(campaign_instance):
...
while True:
try:
log.info("start_campaign() Requests in Queue: {}".format(len(web_requests)))
web_request = web_requests.pop()
time.sleep(delay)
headers = {'Content-Type': 'application/json'}
request = httpclient.HTTPRequest(auth_username=settings.api_account,
auth_password=settings.api_password,
url=settings.api_url,
body=json.dumps(web_request),
headers=headers,
request_timeout=15,
method="POST")
response = yield http_client.fetch(request)
except httpclient.HTTPError, e:
log.exception("start_campaign() " + str(e))
except IndexError:
log.info('start_campaign() Campaign web requests completed. Errors {}'.format(api_errors))
break
但似乎在继续之前等待HTTP响应。
答案 0 :(得分:0)
您可以尝试:
myfree()
重复使用while循环:
class WebRequest(RequestHandler):
def __init__(self, web_request):
self.delay = 0
self.web_request = web_request
@asynchronous
def post(self):
IOLoop.instance().add_timeout(self.delay, self._process)
@gen.coroutine
def _process(self):
try:
http_client = httpclient.AsyncHTTPClient()
log.info("start_campaign() Web request: {}".format(self.web_request))
headers = {'Content-Type': 'application/json'}
request = httpclient.HTTPRequest(auth_username=settings.api_account,
auth_password=settings.api_password,
url=settings.api_url,
body=json.dumps(self.web_request),
headers=headers,
request_timeout=15,
method="POST")
response = yield http_client.fetch(request)
except Exception, exception:
log.exception(exception)