Skip to content

Instantly share code, notes, and snippets.

@tylerburdsall
Last active December 23, 2022 19:34
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tylerburdsall/ef54430778ff5bbbc2ceced2453fed17 to your computer and use it in GitHub Desktop.
Save tylerburdsall/ef54430778ff5bbbc2ceced2453fed17 to your computer and use it in GitHub Desktop.
Example showing asynchronous HTTP request with Python 3.5.0 + asyncio
import requests
import asyncio
from concurrent.futures import ThreadPoolExecutor
from timeit import default_timer
START_TIME = default_timer()
def fetch(session, csv):
base_url = "https://people.sc.fsu.edu/~jburkardt/data/csv/"
with session.get(base_url + csv) as response:
data = response.text
if response.status_code != 200:
print("FAILURE::{0}".format(url))
elapsed = default_timer() - START_TIME
time_completed_at = "{:5.2f}s".format(elapsed)
print("{0:<30} {1:>20}".format(csv, time_completed_at))
return data
async def get_data_asynchronous():
csvs_to_fetch = [
"ford_escort.csv",
"cities.csv",
"hw_25000.csv",
"mlb_teams_2012.csv",
"nile.csv",
"homes.csv",
"hooke.csv",
"lead_shot.csv",
"news_decline.csv",
"snakes_count_10000.csv",
"trees.csv",
"zillow.csv"
]
print("{0:<30} {1:>20}".format("File", "Completed at"))
with ThreadPoolExecutor(max_workers=10) as executor:
with requests.Session() as session:
# Set any session parameters here before calling `fetch`
loop = asyncio.get_event_loop()
START_TIME = default_timer()
tasks = [
loop.run_in_executor(
executor,
fetch,
*(session, csv) # Allows us to pass in multiple arguments to `fetch`
)
for csv in csvs_to_fetch
]
for response in await asyncio.gather(*tasks):
pass
def main():
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(get_data_asynchronous())
loop.run_until_complete(future)
main()
@adityathebe
Copy link

Is there some memory leaks in this code ?

@tylerburdsall
Copy link
Author

There shouldn't be, if you are referring to https://bugs.python.org/issue38430. We want to make a list of Futures (line 42) so that we can kick off each process asynchronously. We use await on line 50 when getting the results of each task.

If you do have any data to show that this leaks memory I'm happy to help pin it down!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment