def get_comments_of_top_stories(loop, session, limit, iteration):
"""Retrieve top stories in HN.
"""
fetcher = URLFetcher() # create a new fetcher for this task
try:
response = await fetcher.fetch(session, TOP_STORIES_URL)
except BoomException as e:
log.error("Error retrieving top stories: {}".format(e))
# return instead of re-raising as it will go unnoticed
return
except Exception as e: # catch generic exceptions
log.error("Unexpected exception: {}".format(e))
return
tasks = {
asyncio.ensure_future(
post_number_of_comments(loop, session, fetcher, post_id)
): post_id for post_id in response[:limit]}
# return on first exception to cancel any pending tasks
done, pending = await asyncio.wait(
tasks.keys(), return_when=FIRST_EXCEPTION)
# if there are pending tasks is because there was an exception
# cancel any pending tasks
for pending_task in pending:
pending_task.cancel()
# process the done tasks
for done_task in done:
# if an exception is raised one of the Tasks will raise
try:
print("Post {} has {} comments ({})".format(
tasks[done_task], done_task.result(), iteration))
except BoomException as e:
print("Error retrieving comments for top stories: {}".format(e))
return fetcher.fetch_counter
03_cancelling_coroutines.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录