├── .env ├── .gitignore ├── README.md ├── async_run.py ├── async_run_v2.py ├── av_async_loop.py ├── av_async_run.py ├── av_async_single_run.py ├── av_requests.py └── requirements.txt /.env: -------------------------------------------------------------------------------- 1 | export ALPHAVANTAGE_API_KEY='YOUR KEY HERE' 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Async Examples 2 | 3 | # Quickstart 4 | 5 | ```bash 6 | git clone https://github.com/PatrickAlphaC/async-python 7 | cd async-python 8 | pip install -r requirements.txt 9 | ``` 10 | Get a free API key from [Alpha Vantage](https://www.alphavantage.co/support/#api-key) and set it as an [environment variable](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html). If you're unfamiliar with environment variables, set it in your `.env` file. 11 | 12 | ```bash 13 | export ALPHAVANTAGE_API_KEY='YOUR KEY HERE' 14 | ``` 15 | 16 | The free Alpha Vantage API key is rate limited to 5 API calls/minute. If you'd like to speed test APIs, you can swap it out for a different API, like [this json dummy api.](https://jsonplaceholder.typicode.com/) 17 | 18 | Then run: 19 | ``` 20 | python av_requests.py 21 | ``` 22 | And you'll get an output like: 23 | ``` 24 | Timer started... 25 | It took 1.1849939823150635 seconds to make 5 API calls 26 | ``` 27 | 28 | To run it async, run: 29 | ``` 30 | python av_async_run.py 31 | ``` 32 | and you'll get an output like: 33 | ``` 34 | Timer started... 35 | Time to make 5 API calls with tasks, it took: 0.400589227676391 36 | ``` 37 | 38 | -------------------------------------------------------------------------------- /async_run.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import aiohttp 3 | import os 4 | import time 5 | # To work with the .env file 6 | from dotenv import load_dotenv 7 | load_dotenv() 8 | 9 | api_key = os.getenv('ALPHAVANTAGE_API_KEY') 10 | url = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 11 | symbols = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'AAPL'] 12 | results = [] 13 | 14 | start = time.time() 15 | async def get_symbols(): 16 | async with aiohttp.ClientSession() as session: 17 | for symbol in symbols: 18 | print('Working on symbol {}'.format(symbol)) 19 | response = await asyncio.create_task(session.get(url.format(symbol, api_key), ssl=False)) 20 | results.append(await response.json()) 21 | 22 | asyncio.run(get_symbols()) 23 | # loop = asyncio.get_event_loop() 24 | # loop.run_until_complete(get_symbols()) 25 | # loop.close() 26 | end = time.time() 27 | total_time = end - start 28 | print("It took {} seconds to make {} API calls".format(total_time, len(symbols))) 29 | print('You did it!') 30 | -------------------------------------------------------------------------------- /async_run_v2.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | import aiohttp 3 | import os 4 | import time 5 | # To work with the .env file 6 | from dotenv import load_dotenv 7 | load_dotenv() 8 | 9 | api_key = os.getenv('ALPHAVANTAGE_API_KEY') 10 | url = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 11 | symbols = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'AAPL'] 12 | results = [] 13 | 14 | start = time.time() 15 | 16 | def get_tasks(session): 17 | tasks = [] 18 | for symbol in symbols: 19 | tasks.append(asyncio.create_task(session.get(url.format(symbol, api_key), ssl=False))) 20 | return tasks 21 | 22 | async def get_symbols(): 23 | async with aiohttp.ClientSession() as session: 24 | tasks = get_tasks(session) 25 | # you could also do 26 | # tasks = [session.get(URL.format(symbol, API_KEY), ssl=False) for symbol in symbols] 27 | responses = await asyncio.gather(*tasks) 28 | # for response in responses: 29 | # results.append(await response.json()) 30 | 31 | asyncio.run(get_symbols()) 32 | 33 | end = time.time() 34 | total_time = end - start 35 | print("It took {} seconds to make {} API calls".format(total_time, len(symbols))) 36 | print('You did it!') 37 | -------------------------------------------------------------------------------- /av_async_loop.py: -------------------------------------------------------------------------------- 1 | import aiohttp 2 | import asyncio 3 | import os 4 | import time 5 | # To work with the .env file 6 | from dotenv import load_dotenv 7 | load_dotenv() 8 | 9 | API_KEY = os.getenv('ALPHAVANTAGE_API_KEY') 10 | URL = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 11 | SYMBOLS = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'GOOGL'] 12 | results = [] 13 | 14 | 15 | def get_tasks(session): 16 | tasks = [] 17 | for symbol in SYMBOLS: 18 | tasks.append(session.get(URL.format(symbol, API_KEY), ssl=False)) 19 | return tasks 20 | 21 | 22 | async def run_tasks(): 23 | session = aiohttp.ClientSession() 24 | # tasks = get_tasks(session) 25 | tasks = [session.get(URL.format(symbol, API_KEY), ssl=False) for symbol in SYMBOLS] 26 | 27 | # Can't do this!! 28 | # for symbol in symbols: 29 | # tasks.append(session.get(url.format(symbol, api_key))) 30 | responses = await asyncio.gather(*tasks) 31 | for response in responses: 32 | results.append(await response.json()) 33 | await session.close() 34 | 35 | print("Timer started...") 36 | start = time.time() 37 | # asyncio.run(run_tasks()) 38 | loop = asyncio.get_event_loop() 39 | results = loop.run_until_complete(run_tasks()) 40 | loop.close() 41 | end = time.time() 42 | total_time = end - start 43 | print( 44 | f"Time to make {len(SYMBOLS)} API calls with tasks, it took: {total_time}") 45 | -------------------------------------------------------------------------------- /av_async_run.py: -------------------------------------------------------------------------------- 1 | import aiohttp 2 | import asyncio 3 | import os 4 | import time 5 | # To work with the .env file 6 | from dotenv import load_dotenv 7 | load_dotenv() 8 | 9 | API_KEY = os.getenv('ALPHAVANTAGE_API_KEY') 10 | URL = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 11 | SYMBOLS = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'PEP'] 12 | results = [] 13 | 14 | 15 | def get_tasks(session): 16 | tasks = [] 17 | for symbol in SYMBOLS: 18 | tasks.append(session.get(URL.format(symbol, API_KEY), ssl=False)) 19 | return tasks 20 | 21 | 22 | async def run_tasks(): 23 | session = aiohttp.ClientSession() 24 | tasks = get_tasks(session) 25 | # You could also use: 26 | # tasks = [session.get(URL.format(symbol, API_KEY), ssl=False) for symbol in SYMBOLS] 27 | 28 | # Can't do this!! 29 | # It has to be a one-liner 30 | # for symbol in symbols: 31 | # tasks.append(session.get(url.format(symbol, api_key))) 32 | responses = await asyncio.gather(*tasks) 33 | for response in responses: 34 | results.append(await response.json()) 35 | await session.close() 36 | 37 | print("Timer started...") 38 | start = time.time() 39 | asyncio.run(run_tasks()) 40 | end = time.time() 41 | total_time = end - start 42 | print( 43 | f"Time to make {len(SYMBOLS)} API calls with tasks, it took: {total_time}") 44 | -------------------------------------------------------------------------------- /av_async_single_run.py: -------------------------------------------------------------------------------- 1 | import aiohttp 2 | import asyncio 3 | import os 4 | # To work with the .env file 5 | from dotenv import load_dotenv 6 | load_dotenv() 7 | 8 | API_KEY = os.getenv('ALPHAVANTAGE_API_KEY') 9 | URL = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 10 | SYMBOL = 'AAPL' 11 | results = [] 12 | 13 | async def run_tasks(): 14 | session = aiohttp.ClientSession() 15 | responses = await session.get(URL.format(SYMBOL, API_KEY), ssl=False) 16 | await session.close() 17 | 18 | asyncio.run(run_tasks()) 19 | 20 | -------------------------------------------------------------------------------- /av_requests.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import os 3 | import time 4 | # To work with the .env file 5 | from dotenv import load_dotenv 6 | load_dotenv() 7 | 8 | api_key = os.getenv('ALPHAVANTAGE_API_KEY') 9 | url = 'https://www.alphavantage.co/query?function=OVERVIEW&symbol={}&apikey={}' 10 | symbols = ['AAPL', 'GOOG', 'TSLA', 'MSFT', 'PEP'] 11 | results = [] 12 | 13 | 14 | def run_tasks(): 15 | for symbol in symbols: 16 | response = requests.get(url.format(symbol, api_key)) 17 | results.append(response.json()) 18 | 19 | 20 | print("Timer started...") 21 | start = time.time() 22 | run_tasks() 23 | end = time.time() 24 | total_time = end - start 25 | print("It took {} seconds to make {} API calls".format(total_time, len(symbols))) 26 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-dotenv 2 | aiohttp 3 | asyncio 4 | requests 5 | --------------------------------------------------------------------------------