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 = [post_number_of_comments(
loop, session, fetcher, post_id) for post_id in response[:limit]]
# return on first exception to cancel any pending tasks
done, pending = await asyncio.wait(tasks, return_when=FIRST_EXCEPTION)
# cancel any pending tasks, the tuple could be empty so it's safe
for pending_task in pending:
pending_task.cancel()
# process the done tasks
for done_task in done:
# one of the Tasks could raise an exception
try:
print("Post ??? has {} comments ({})".format(
done_task.result(), iteration))
except BoomException as e:
print("Error retrieving comments for top stories: {}".format(e))
return fetcher.fetch_counter
01_cancelling_coroutines.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录