Member-only story

Graphical exploration of concurrency in Python

Part 4: Async API requests with Python and httpx

A series exploring concurrency in Python to speed up data retrieval from remote APIs.

Derek
3 min readJul 12, 2020

--

This article is part of a series: Graphical exploration of concurrency in Python. Part 1: Defining and timing an API function in Python.

Recap: Sequential vs. Multiprocessing vs. Threading

In the last section, we moved from running requests on multiple processes, each with their own process id and memory allocation to multiple threads running under the pre-existing process that’s running our code. This showed an improvement in overall runtime by eliminating the startup time of all those processes.

In this section we’ll run all the requests on a single thread and, instead of Python’s threading library handling the coordination of concurrency, we’ll use a combination of asyncio and httpx.

asyncio and httpx

At this point, we need to update jira_query() slightly to make it async and await ready.

  • The first change is to add async in front of the function definition. This tells python that this function can be awaited. Now if we were to call this function normally results = jira_query_async(...) then results would look like this <coroutine object jira_query_async at 0x7f2b985023b0>. That’s because…

--

--

Derek
Derek

Written by Derek

I write about programming, statistics and tech. Studied electrical engineering (B.Sc., M.Sc.) and I work in software.

No responses yet

Write a response