Tech spotlight: How to implement async requests in your Python code
Tech spotlight: How to implement async requests in your Python code
copy gray icon

Tech spotlight: How to implement async requests in your Python code

January 6, 2022

Overview

Traditionally, Python has been a blocking language when it comes to I/O and networking, meaning lines of code are executed one at a time. With network requests, we have to wait for the response before we can make another request. Meanwhile, our computers sit idling. Luckily, there are ways to address this, the most exciting of which is leveraging asynchronous requests with the aiohttp package. This article explains how asynchronicity can help solve these issues – and how you can put it into place within your own code!

Background: Python and Asynchronicity

Although Python is no stranger to asynchronicity with its multiprocessing package dating back to 2008, it didn't quite achieve the same status as the async/await paradigm in Javascript. With the release of Python 3.7, the async/await syntax has put our computers back to work and allowed for code to be performed concurrently.  We can now fire off all our requests at once and grab the responses as they come in.

Multiprocessing enables a different level of asynchronicity than the async/await paradigm. Python’s multiprocessing package enables multi-core processing. This means the same code can run concurrently on separate processes without blocking one another. This allows for true parallelism of CPU-bound tasks. It can be overkill if you require concurrency on tasks that are simply I/O bound which the async/await paradigm is better suited for.

Asynchronous Pinwheels

At Pinwheel, we have an API for retrieving payroll data such as Paystubs, Income, Identity, Shifts, Employment, and Direct Deposit Allocations data. It takes no stretch of the imagination that getting this info can involve a lot of requests.

Pinwheel uses async/await to concurrently retrieve payroll data. Implementing a project with asynchronous requests can yield enormous benefits by reducing latency. Using asynchronous requests has reduced the time it takes to retrieve a user's payroll info by up to 4x. To see async requests in action we can write some code to make a few requests. Read on to learn how to leverage asynchronous requests to speed-up python code.

Trying out async/await

Let's write some code that makes parallel requests. To test this we can use the Coinbase API to get the current prices of various cryptocurrencies.

Coroutine Maintenance

The aiohttp package is emerging as the standard for handling asynchronous HTTP requests. To get started we can install the aiohttp package.

$ pip install aiohttp

view rawinstall.sh hosted with ❤ by GitHub

We can leverage aiohttp and the builtin aysncio package to make requests. Our first function that makes a simple GET request will create in async land what is called a coroutine. Coroutines are created when we combine the async and await syntax.

async def get_url(session: aiohttp.ClientSession, url: str) -> Dict:    async with session.get(url) as response:        return await response.json()

view rawget_url.py hosted with ❤ by GitHub

In the above example (modeled off of the aiohttp request lifecycle example from the docs) we take in aiohttp's ClientSession and a URL as arguments and call .get() on the ClientSession. One of the big differences between aiohttp and the old school requests package is that the response attributes live inside a context manager (under the hood the dunder methods __aenter__ and __aexit__ are being called). That means if we want to do something with status codes or response history we need to do so within this block.

...async with session.get(url) as response:    if response.status == 503: # do some work

view rawstatus_code.py hosted with ❤ by GitHub

If we are making a request to an endpoint that returns JSON content we would naturally like to turn the response into a python dictionary. In the function above we use .json() to do just that. We could also await .text() to turn HTML into a string or even .read() to handle byte content (ie. pdfs maybe?).

What about POST? Swap out .get() for .post() and then pass whatever payload you need in the data kwarg.

async with session.post(url, data=payload) as response:    ...

view rawpost.py hosted with ❤ by GitHub

A List of Tasks

The next step is to set up session persistence that we can maintain in all our requests. Luckily aiohttp's ClientSession allows us to do this. The following code block creates the Client Session to pass into the get_url function.

async def request_urls(urls: List[str]):    async with aiohttp.ClientSession() as session:        tasks: List[asyncio.Task] = []        for url in urls:            tasks.append(                asyncio.ensure_future(                    get_url(session, url)                )            )        return await asyncio.gather(*tasks)

view rawtasks.py hosted with ❤ by GitHub

We are again using a context manager but this time to handle the session. The great thing about reusing the ClientSession like this is that any headers or cookies passed to the session will be used for all of our requests. When we instantiate the ClientSession this is where we can pass headers, cookies, or a TraceConfig object (for logging!) as kwargs.

async with aiohttp.ClientSession(    headers=headers_dict,    cookies=cookies_dict,    trace_configs=[trace_config],) as session:    ...

view rawheaders_cookies.py hosted with ❤ by GitHub

Back to the Future

Next we are creating a list of tasks to execute. Asyncio's method ensure_future allows for coroutines to be turned into Tasks so they are not immediately called. In our case we want the coroutine get_url that we wrote above to be converted into a Task.

task: asyncio.Task = asyncio.ensure_future(    get_url(session, url))

view rawfuture.py hosted with ❤ by GitHub

The tasks are then passed to asyncio's gather which schedules each coroutine.

async def request_urls(urls: List[str]):    tasks: List[asyncio.Task] = []    ...        return await asyncio.gather(*tasks)

view rawgather.py hosted with ❤ by GitHub

The final step is to pass the request_urls function to Asyncio's run method. Remember that request_urls is in fact just a coroutine defined by the async/await syntax. This asyncio method will execute our coroutine and concurrently execute the scheduled tasks.

responses: List[Dict] = asyncio.run(request_urls(urls))

view rawresponses.py hosted with ❤ by GitHub

All Together Now

Let's see it all together! Here is some code that makes concurrent requests to the Coinbase API to get crypto to USD exchange rates.

import asyncioimport aiohttpCRYPTOS: List[str] = [    "BTC",    "ETH",    "DOGE",    "BCH",    "ETC",    "LTC",]URLS: List[str] = [    f"https://api.coinbase.com/v2/prices/{crypto}-USD/buy"    for crypto in CRYPTOS]async def request_urls(urls: List[str]):    async with aiohttp.ClientSession() as session:        tasks: List[asyncio.Task] = []        for url in urls:            tasks.append(                asyncio.ensure_future(                    get_url(session, url)                )            )        return await asyncio.gather(*tasks)async def get_url(session: aiohttp.ClientSession, url: str) -> Dict:    async with session.get(url) as response:        return await response.json()responses: List[Dict] = asyncio.run(request_urls(URLS))print(responses)

view rawasync_crypto.py hosted with ❤ by GitHub

Running the code, we get the list of dicts printed to the console. The best part is that the responses are in the same order as our urls. Even if the last endpoint comes in first aiohttp doesn't reorder our list.

$ python3 async_crypto.py[{'data': {'base': 'BTC', 'currency': 'USD', 'amount': '58995.78'}}, {'data': {'base': 'ETH', 'currency': 'USD', 'amount': '4467.16'}}, {'data': {'base': 'DOGE', 'currency': 'USD', 'amount': '0.22'}}, {'data': {'base': 'BCH', 'currency': 'USD', 'amount': '582.49'}}, {'data': {'base': 'ETC', 'currency': 'USD', 'amount': '48.59'}}, {'data': {'base': 'LTC', 'currency': 'USD', 'amount': '208.84'}}]

view rawgistfile1.sh hosted with ❤ by GitHub

So what kind of benefits can we see? To see the difference in latency let's do an A/B test, comparing sync requests using pythons requests package vs async requests with aiohttp. To do this we can use the time package to measure the difference in seconds for the requests approach below and the async request_urls function.

from time import timeresponses: List[Dict] = []start_time: float = time()for url in URLS:    responses.append(requests.get(url).json())end_time: float = time()print(end_time - start_time)

view rawtime_test.py hosted with ❤ by GitHub

Looking at the average time it takes to request endpoints sync vs. async, even with the small list of six URLs, we can see a clear winner. One can only imagine the benefits when a list of URLs is much larger!

And that's it! Asynchronous requests using aiohttp is a great tool for speeding up your code. There are many other options for executing code concurrently and plenty of use cases for making requests one at a time. But concurrent code is becoming a bigger part of Python and understanding asynchronicity is a powerful asset.

Always stay up to date

View our Privacy Policy   ➔
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Up next

The digital gap threatening credit unions—and how to close it

The digital gap threatening credit unions—and how to close it

Read more  ➔
Are Gen Z and Millennials your most valuable—and most underserved—banking customers?

Are Gen Z and Millennials your most valuable—and most underserved—banking customers?

Read more  ➔
Pinwheel Helping Power Robinhood Banking Deposits

Pinwheel Helping Power Robinhood Banking Deposits

Read more  ➔
Visa Launches Enhanced Subscription Manager, Giving Consumers Greater Control Over Recurring Payments

Visa Launches Enhanced Subscription Manager, Giving Consumers Greater Control Over Recurring Payments

Read more  ➔
OnePay Partners with Pinwheel

OnePay Partners with Pinwheel

Read more  ➔
Leighanne Levensaler joins Pinwheel as strategic advisor

Leighanne Levensaler joins Pinwheel as strategic advisor

Read more  ➔
River launches direct deposit powered by Pinwheel Deposit Switch

River launches direct deposit powered by Pinwheel Deposit Switch

Read more  ➔
KYC’s Killer App

KYC’s Killer App

Read more  ➔
J.D. Power confirms ROI of bank account "Soft Switching"

J.D. Power confirms ROI of bank account "Soft Switching"

Read more  ➔
Pinwheel welcomes Don Weinstein as an advisor

Pinwheel welcomes Don Weinstein as an advisor

Read more  ➔
Future of account onboarding

Future of account onboarding

Read more  ➔
How we help lenders unlock the value of Pay by Paycheck

How we help lenders unlock the value of Pay by Paycheck

Read more  ➔
Pay By Paycheck: The next big thing in lending

Pay By Paycheck: The next big thing in lending

Read more  ➔
A new chapter for Pinwheel

A new chapter for Pinwheel

Read more  ➔
Hey - remember us from tax season?

Hey - remember us from tax season?

Read more  ➔
Narmi x Pinwheel expand partnership with the Switch Kit

Narmi x Pinwheel expand partnership with the Switch Kit

Read more  ➔
Winning the war for primacy with digital innovation

Winning the war for primacy with digital innovation

Read more  ➔
Did the CFPB Eat Your Homework?

Did the CFPB Eat Your Homework?

Read more  ➔
Achieving loyalty with a differentiated digital experience

Achieving loyalty with a differentiated digital experience

Read more  ➔
​Revolutionizing digital banking: How Bill Manager drives engagement and growth

​Revolutionizing digital banking: How Bill Manager drives engagement and growth

Read more  ➔
Pinwheel expands PreMatch coverage to 45M Americans with addition of Paychex partnership

Pinwheel expands PreMatch coverage to 45M Americans with addition of Paychex partnership

Read more  ➔
Pinwheel Pulse: Q1 2025

Pinwheel Pulse: Q1 2025

Read more  ➔
Citadel Direct Deposit Manager is powered by Pinwheel

Citadel Direct Deposit Manager is powered by Pinwheel

Read more  ➔
Security Bank of Kansas City upgrades account onboarding with Pinwheel

Security Bank of Kansas City upgrades account onboarding with Pinwheel

Read more  ➔
Pinwheel partners with MoneyLion to power Direct Deposit Switching

Pinwheel partners with MoneyLion to power Direct Deposit Switching

Read more  ➔
Consumer Banking Sentiment 2025

Consumer Banking Sentiment 2025

Read more  ➔
Q&A with Meriwest Head of Digital Strategy, Gene Fichtenholz

Q&A with Meriwest Head of Digital Strategy, Gene Fichtenholz

Read more  ➔
Roster Mentality & Retail Banking

Roster Mentality & Retail Banking

Read more  ➔
Achieve primacy day one

Achieve primacy day one

Read more  ➔
Pinwheel Pulse: 2024 Year in Review

Pinwheel Pulse: 2024 Year in Review

Read more  ➔
Why credit unions have a neighborhood advantage

Why credit unions have a neighborhood advantage

Read more  ➔
The Product Pulse

The Product Pulse

Read more  ➔
Introducing Bill Manager

Introducing Bill Manager

Read more  ➔
PreMatch results are in

PreMatch results are in

Read more  ➔
How we achieve the industry’s best conversion rates

How we achieve the industry’s best conversion rates

Read more  ➔
Automated direct deposit is powering the next generation of growth for credit unions

Automated direct deposit is powering the next generation of growth for credit unions

Read more  ➔
Nassau Financial Credit Union Selects Pinwheel As Direct Deposit Switch Partner

Nassau Financial Credit Union Selects Pinwheel As Direct Deposit Switch Partner

Read more  ➔
SafeLink expands access to frictionless experiences

SafeLink expands access to frictionless experiences

Read more  ➔
Industry leaders talk consumer bank switching behaviors

Industry leaders talk consumer bank switching behaviors

Read more  ➔
The branch of the future

The branch of the future

Read more  ➔
Giving credit where it’s due

Giving credit where it’s due

Read more  ➔
Trust and Verify

Trust and Verify

Read more  ➔
Citizens & Pinwheel talk primacy

Citizens & Pinwheel talk primacy

Read more  ➔
Be the Amazon of banks  

Be the Amazon of banks  

Read more  ➔
Enhancing digital trust: Inside Pinwheel's commitment to security

Enhancing digital trust: Inside Pinwheel's commitment to security

Read more  ➔
Who’s making money moves in 2024?

Who’s making money moves in 2024?

Read more  ➔
Consumer bank switching behavior demystified

Consumer bank switching behavior demystified

Read more  ➔
The metrics you care about the most are now available in real-time

The metrics you care about the most are now available in real-time

Read more  ➔
New Jack Henry partnership makes it easier for community banks to take advantage of Pinwheel

New Jack Henry partnership makes it easier for community banks to take advantage of Pinwheel

Read more  ➔
 Pinwheel's CMO discusses bank competition for primacy in 2024

Pinwheel's CMO discusses bank competition for primacy in 2024

Read more  ➔
Introducing the next generation of Automated Direct Deposit Switching

Introducing the next generation of Automated Direct Deposit Switching

Read more  ➔
Fraud Fighers Chapter 1: Know Your Fraudster

Fraud Fighers Chapter 1: Know Your Fraudster

Read more  ➔
Know Your Fraudster Q&A with Robert Reynolds

Know Your Fraudster Q&A with Robert Reynolds

Read more  ➔
This is how banks close the loop with branch guests: Introducing Pinwheel Smart Branch

This is how banks close the loop with branch guests: Introducing Pinwheel Smart Branch

Read more  ➔
Introducing Pinwheel Deposit Switch 2.0, a revolutionary upgrade that maximizes coverage and conversion for every US worker

Introducing Pinwheel Deposit Switch 2.0, a revolutionary upgrade that maximizes coverage and conversion for every US worker

Read more  ➔
Key factors to consider before implementing a payroll connectivity API

Key factors to consider before implementing a payroll connectivity API

Read more  ➔
Enhance credit line management with income data

Enhance credit line management with income data

Read more  ➔
See your customers’ earnings weeks into the future with projected earnings

See your customers’ earnings weeks into the future with projected earnings

Read more  ➔
How to reduce default risk with consumer-permissioned data

How to reduce default risk with consumer-permissioned data

Read more  ➔
Digital lending technologies and trends that are shaping the industry

Digital lending technologies and trends that are shaping the industry

Read more  ➔
4 technologies that improve fraud detection in banking

4 technologies that improve fraud detection in banking

Read more  ➔
Why automated income verification is a must-have feature for lenders

Why automated income verification is a must-have feature for lenders

Read more  ➔
December product release: 10% increase in conversion, enhanced security and access to pay frequency data

December product release: 10% increase in conversion, enhanced security and access to pay frequency data

Read more  ➔
A conversation with our Chief Information Security Officer

A conversation with our Chief Information Security Officer

Read more  ➔
Former CFPB Deputy Director Raj Date Joins Pinwheel as an Advisor

Former CFPB Deputy Director Raj Date Joins Pinwheel as an Advisor

Read more  ➔
Cash flow underwriting: Benefits & how to access cash flow data

Cash flow underwriting: Benefits & how to access cash flow data

Read more  ➔
Why banks need a payroll connectivity API that prioritizes information security

Why banks need a payroll connectivity API that prioritizes information security

Read more  ➔
How alternative credit data can benefit lenders

How alternative credit data can benefit lenders

Read more  ➔
Tech Spotlight: Implementing your first feature flag

Tech Spotlight: Implementing your first feature flag

Read more  ➔
Pinwheel Welcomes New Advisor, Ethan Yeh, to Advance Pinwheel’s Data Science Strategy

Pinwheel Welcomes New Advisor, Ethan Yeh, to Advance Pinwheel’s Data Science Strategy

Read more  ➔
Tech spotlight: Securing access control across internal services

Tech spotlight: Securing access control across internal services

Read more  ➔
The anatomy and potential of payroll data: Transforming complex data into insights

The anatomy and potential of payroll data: Transforming complex data into insights

Read more  ➔
Beyond the credit score: Propelling consumer finance into the future with income data

Beyond the credit score: Propelling consumer finance into the future with income data

Read more  ➔
Ayokunle (Ayo) Omojola joins Pinwheel’s Board of Directors

Ayokunle (Ayo) Omojola joins Pinwheel’s Board of Directors

Read more  ➔
Conquering conversion: Engineering practices developed to help customers

Conquering conversion: Engineering practices developed to help customers

Read more  ➔
Driving Customer Delight: From implementation and beyond

Driving Customer Delight: From implementation and beyond

Read more  ➔
Pinwheel Supports Open Finance Data Security Standard

Pinwheel Supports Open Finance Data Security Standard

Read more  ➔
How we design Pinwheel to solve real customer problems

How we design Pinwheel to solve real customer problems

Read more  ➔
What is consumer-permissioned data and what are its benefits?

What is consumer-permissioned data and what are its benefits?

Read more  ➔
How payroll data connectivity can help financial service providers in tumultuous market conditions

How payroll data connectivity can help financial service providers in tumultuous market conditions

Read more  ➔
Pinwheel now supports document uploads to supplement payroll data

Pinwheel now supports document uploads to supplement payroll data

Read more  ➔
Brian Karimi-Pashaki joins Pinwheel as Partnerships Lead

Brian Karimi-Pashaki joins Pinwheel as Partnerships Lead

Read more  ➔
Optimizing for conversion with smarter employer mappings

Optimizing for conversion with smarter employer mappings

Read more  ➔
What are super apps and how will they impact financial services?

What are super apps and how will they impact financial services?

Read more  ➔
Increase conversions and maximize share of wallet with Pinwheel's new UX update

Increase conversions and maximize share of wallet with Pinwheel's new UX update

Read more  ➔
Pinwheel announces support for taxes

Pinwheel announces support for taxes

Read more  ➔
Ryan Nier Joins Pinwheel as the Company’s first General Counsel

Ryan Nier Joins Pinwheel as the Company’s first General Counsel

Read more  ➔
The future of enabling earned wage access

The future of enabling earned wage access

Read more  ➔
Deliver earned wage access faster with Pinwheel Earnings Stream

Deliver earned wage access faster with Pinwheel Earnings Stream

Read more  ➔
Digital transformation in banking in 2022: What it means, trends & examples

Digital transformation in banking in 2022: What it means, trends & examples

Read more  ➔
June product release: Expanded connectivity to employers, a custom experience with Link API and more

June product release: Expanded connectivity to employers, a custom experience with Link API and more

Read more  ➔
Pinwheelie Spotlight: LaRena Iocco, Software Engineer

Pinwheelie Spotlight: LaRena Iocco, Software Engineer

Read more  ➔
Build fully custom experiences with Pinwheel’s Link API

Build fully custom experiences with Pinwheel’s Link API

Read more  ➔
Pinwheel expands connectivity to 1.5M employers

Pinwheel expands connectivity to 1.5M employers

Read more  ➔
Robert Reynolds joins Pinwheel as Head of Product

Robert Reynolds joins Pinwheel as Head of Product

Read more  ➔
Pinwheel obtains highest security certification in the industry

Pinwheel obtains highest security certification in the industry

Read more  ➔
Lauren Crossett becomes Pinwheel’s first Chief Revenue Officer

Lauren Crossett becomes Pinwheel’s first Chief Revenue Officer

Read more  ➔
Everything you should know about the role of APIs in banking

Everything you should know about the role of APIs in banking

Read more  ➔
Open finance: What is it and how does it impact financial services?

Open finance: What is it and how does it impact financial services?

Read more  ➔
How automated direct deposit switching benefits traditional banks

How automated direct deposit switching benefits traditional banks

Read more  ➔