def list_threads():
def order(item):<EOL><INDENT>"""<STR_LIT>"""<EOL>thread = item[<NUM_LIT:1>]<EOL>timestamp = thread['<STR_LIT>']<EOL>return thread.get('<STR_LIT>', {}).get('<STR_LIT>') or timestamp<EOL><DEDENT>with content.pipeline() as pipe:<EOL><INDENT>uids = []<EOL>for thread_uid in links.lrange(build_key(THREADS_KEY), <NUM_LIT:0>, -<NUM_LIT:1>):<EOL><INDENT>pipe.hgetall(build_key(THREAD_KEY, thread_uid))<EOL>uids.append(thread_uid)<EOL><DEDENT>threads = dict(zip(uids, pipe.execute()))<EOL><DEDENT>comments_request = OrderedDict()<EOL>for thread_uid, thread in iteritems(threads):<EOL><INDENT>last_comment_uid = thread.get('<STR_LIT>')<EOL>if not last_comment_uid:<EOL><INDENT>continue<EOL><DEDENT>comments_request[thread_uid] = thread['<STR_LIT>']<EOL><DEDENT>if comments_request:<EOL><INDENT>with links.pipeline() as pipe:<EOL><INDENT>for thread_uid in iterkeys(comments_request):<EOL><INDENT>pipe.get(build_key(THREAD_COUNTER_KEY, thread_uid))<EOL><DEDENT>response = zip(iterkeys(comments_request), pipe.execute())<EOL><DEDENT>for thread_uid, counter in response:<EOL><INDENT>threads[thread_uid]['<STR_LIT>'] = counter<EOL><DEDENT>with content.pipeline() as pipe:<EOL><INDENT>for thread_uid, comment_uid in iteritems(comments_request):<EOL><INDENT>key = build_key(COMMENT_KEY, thread_uid, comment_uid)<EOL>pipe.hgetall(key)<EOL><DEDENT>response = zip(iterkeys(comments_request), pipe.execute())<EOL><DEDENT>for thread_uid, comment in response:<EOL><INDENT>threads[thread_uid]['<STR_LIT>'] = comment<EOL><DEDENT><DEDENT>return OrderedDict(sorted(iteritems(threads), key=order, reverse=True))<EOL>
List all available threads in most efficient way.