Skip to content

Added asycn requests

Raj Singh Gogia requested to merge added-async-req into main

Created by: waraich1

  • Earlier, we were making sync requests like this
subreddit = self.reddit.subreddit(subredditName)
for submission in subreddit.hot(limit=num):
	post_comment = comment.get_comment(submission.id, "hot", False, self.token)
res.extend(post_comment)
return res
  • This was causing API to take almost 10 seconds
  • To fix this I used HTTPX library which basically make concurrent requests like this
async  def  do_tasks():
     async  with  httpx.AsyncClient() as  client:
         tasks = [client.get(url, headers=headers, follow_redirects=True) for  url  in  res]
         result = await  asyncio.gather(*tasks)
         return  result
res = asyncio.run(do_tasks())

This makes all the get requests concurrently which imroves the time

Merge request reports