I'm new to Tornado so I wanted to know if the code below is a correct way to approach the problem or there is a better one. It works but I'm not sure regarding its efficiency.
The code is based on the documentation here
In the middle of my script, I need to run HTTP Requests (10-50). Apparently, it is possible to do this in parallel this way :
@gen.coroutine
def parallel_fetch_many(urls):
responses = yield [http_client.fetch(url) for url in urls]
# responses is a list of HTTPResponses in the same order
How do I access responses after the coroutine is done ? Can I just add return responses
?
Also, as I only need to use an async process once in my code, I start the IOLoop this way :
# run_sync() doesn't take arguments, so we must wrap the
# call in a lambda.
IOLoop.current().run_sync(lambda: parallel_fetch_many(googleLinks))
Is it correct to do it this way ? Or should I just start the IOLoop at the beginning of the script and stop it a the end, even though I only use an async process once.
Basically, my question is : Is the code below correct ?
@gen.coroutine
def parallel_fetch_many(urls):
responses = yield [http_client.fetch(url) for url in urls]
return responses
googleLinks = [url1,url2,...,urln]
responses = IOLoop.current().run_sync(lambda:parallel_fetch_many(googleLinks))
do_something(responses)
Yes, your code looks correct to me.