Skip to content

Instantly share code, notes, and snippets.

@j-thepac
Created August 14, 2024 07:51
Show Gist options
  • Save j-thepac/a0cc5cec5e13ffe3bc2439d3c5c0fe8d to your computer and use it in GitHub Desktop.
Save j-thepac/a0cc5cec5e13ffe3bc2439d3c5c0fe8d to your computer and use it in GitHub Desktop.
To check the performance of Normal Operation vs MulitThreaded Operations
'''
Excercise for Async Multithreaded vs Normal
Problem :
1. To call 2 api
2. Get the response
3. Convert the response data into pandas Dataframe
RUN 1 :
Time take by Async multithreaded = 0.4613368511199951
Time take by normal = 0.37778782844543457
RUN 2:
Time take by Async multithreaded = 0.1995089054107666
Time take by normal = 0.38010501861572266
RUN 3:
Time take by Async multithreaded = 0.19882512092590332
Time take by normal = 0.38787388801574707
'''
import httpx,requests
import pandas as pd
import json
from datetime import datetime
import asyncio
def nse():
headers={
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
,"accept-language": "en-US,en;q=0.9"
,"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
}
path="https://www.nseindia.com/api/marketStatus"
resp=httpx.get(path,headers=headers)
df=pd.DataFrame(resp.json()["indicativenifty50"],index=[0])
def bse():
path= "https://api.bseindia.com/BseIndiaAPI/api/SensexGraphData/w?index=98&flag=0&sector=&seriesid=R&frd=null&tod=null"
headers={"Referer":"https://www.bseindia.com"}
resp=httpx.get(url=path,headers=headers)
res=resp.json().split("#@#")[1]
df=pd.DataFrame(json.loads(res))
async def main():
t1=datetime.now().timestamp()
res1=asyncio.gather(asyncio.to_thread(nse),asyncio.to_thread(bse))
await res1
print(f"Time take by Async multithreaded = {datetime.now().timestamp() - t1}")
def normal():
t1=datetime.now().timestamp()
nse()
bse()
print(f"Time take by normal = {datetime.now().timestamp() - t1}")
asyncio.run(main())
normal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment