Concurrency in Boto3

Cagdas Ozbey
TysonWorks
Published in
2 min readJul 17, 2019

Asyncio provides a set of tools for concurrent programming in Python. In a very simple sense, it does this by having an event loop execute a collection of tasks, with a key difference being that each task chooses when to yield control back to the event loop. Asyncio is a good fit for IO-bound and high-level structured network code. Boto3 (AWS Python SDK) falls into this category.

A lot of existing libraries are not ready to be used with asyncio out of the box. They may block, or depend on concurrency features not available through the module. It’s still possible to use those libraries in an application based on asyncio by using an executor from concurrent.futures to run the code either in a separate thread or a separate process.

The run_in_executor() method of the event loop takes an executor instance, a regular callable to invoke, and any arguments to be passed to the callable. It returns a Future that can be used to wait for the function to finish its work and return something. If no executor is passed in, a ThreadPoolExecutor is created. This example explicitly creates an executor to limit the number of worker threads it will have available.

A ThreadPoolExecutor starts its worker threads and then calls each of the provided functions once in a thread. This example shows how to combine run_in_executor() and wait() to have a coroutine yield control to the event loop while blocking functions run in separate threads, and then wake back up when those functions are finished.

In this tutorial, we will be using async.io and ThreadPoolExecutor to execute Boto3 methods in a separate thread.

Let’s try something quick, here is the snippet.

Our goal is to describe EC2 instances in multiple AWS regions, we have defined two functions for benchmarking. One is concurrent and the other one is synchronous. We use timeit/default_time to measure execution time in seconds.

As expected, the non-blocking code is much faster(about %430 more) than the blocking code.

When you face performance issues with your Boto3 workflows, give concurrency a shot, it can speed up things tremendously.

Are you ready to enhance your AWS Cloud journey? Head over to our website and book a free consultation call

--

--