-
Notifications
You must be signed in to change notification settings - Fork 45
/
async_run_v2.py
36 lines (30 loc) · 1.04 KB
/
async_run_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import asyncio
import aiohttp
import os
import time
# To work with the .env file
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('ALPHAVANTAGE_API_KEY')
url = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}'
symbols = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'AAPL']
results = []
start = time.time()
def get_tasks(session):
tasks = []
for symbol in symbols:
tasks.append(asyncio.create_task(session.get(url.format(symbol, api_key), ssl=False)))
return tasks
async def get_symbols():
async with aiohttp.ClientSession() as session:
tasks = get_tasks(session)
# you could also do
# tasks = [session.get(URL.format(symbol, API_KEY), ssl=False) for symbol in symbols]
responses = await asyncio.gather(*tasks)
# for response in responses:
# results.append(await response.json())
asyncio.run(get_symbols())
end = time.time()
total_time = end - start
print("It took {} seconds to make {} API calls".format(total_time, len(symbols)))
print('You did it!')