ThreadPoolExecutor Jump-Start

Super Fast Python
0 ratings

Python Code Does Not Have to Be Slow!
(or run in just one thread)

With "Python ThreadPoolExecutor Jump-Start" you will learn how to create concurrent for-loops and execute asynchronous tasks in just 7 lessons.

How Much Faster Could Your Code Run
(if it used 100s or 1,000s of threads)?

How many times has this happened to you...

  • ...you write a program to download many files
  • ...you write a program to execute many database queries
  • ...you write a program to read many files

You use a loop, one iteration per file, per query, etc.

And it is SO SLOW.

You run the script and are frustrated at how long it takes.

Yet, you have 2, 4, 8 or more CPU cores sitting idle.

Using electricity.
Waiting for work.

What a waste!

  1. You could change a slow sequential for-loop into a blazingly-fast concurrent for-loop.
  2. You could change run-and-wait tasks into fire-and-forget asynchronous tasks.

This is possible right now with a lesser-known Python class that offers super-easy-to-use thread-based concurrency (and is already installed on your system).

Concurrency is the Path to Faster Code

Python is a joy to use, but getting Python code to run fast is challenging.

Concurrency is a standard approach to running multiple functions simultaneously.

Python concurrency has a bad reputation. So bad, that many developers believe Python does not support true concurrency.

I'm happy to say that these misconceptions are dead wrong.

Python supports real concurrency with first-class native support for threads and processes.

It always has.

  • ...on all recent Python versions, like Python v2.6+ and v3.0+.
  • ...on all major platforms, like Windows, MacOS, and Linux.
  • ...with all major hardware, like Intel, AMD, ARM, and Apple Silicon.

And most importantly, Python concurrency is easy and fun to use.

The trick is to use the ThreadPoolExecutor class and the right types of tasks.

But What About The GIL...?

Have you heard this before:

"Python doesn't support threads because of the Global Interpreter Lock (GIL)."

Yes, it's true. Python threads are limited by the infamous Global Interpreter Lock (GIL).

But...

Critically, the GIL is released allowing multiple threads to run in parallel.

When is the GIL released?

  • ...the GIL is released when performing blocking I/O
  • ...the GIL is released when calling some third-party libraries
  • ...the GIL does not even exist in some third-party Python interpreters

The first point is key.

The GIL is released when performing blocking IO.

Such as:

  • Reading or writing a file from the hard drive.
  • Reading or writing to standard output, input, or error (stdin, stdout, stderr).
  • Printing a document.
  • Downloading or uploading a file.
  • Querying a server.
  • Querying a database.
  • Taking a photo or recording a video.
  • And so much more.

And the easiest way to use threads is with a ThreadPool.

ThreadPoolExecutor: The Modern Thread Pool Class

The ThreadPoolExecutor class provides easy-to-use thread-based concurrency for IO-bound tasks.

This is not some random third-party library, this is a class provided in the Python standard library (already installed on your system).

The ThreadPoolExecutor class has been in the standard library since Python 2 and has been improved further since Python 3.

This is the class you need to use to make your code run faster.

It is specifically designed for you to run for-loops concurrently.

There's just one problem.

Few developers know about it (or how to use it well).

  • The API documentation is thin at best, providing very little guidance on how to use its features.
  • The API docs are buried deep within the Python API docs, making it impossible to find.
  • The few examples out there on the web are terse and limited.

This is madness!

The perfect class for faster concurrent Python code is right there in the standard library, and very few people know it exists or can locate it, let alone decipher the API documentation.

One group of Python developers know about this class and how to use it well.

Python machine learning developers.

My Story: Who Is Jason Brownlee?

(from Python ML engineer to Python concurrency expert and evangelist)

Hi, I'm Jason Brownlee, Ph.D.

I'm a Python developer, husband, and father to two boys.

(a photo of me in the backyard, taken by my wife)

I want to share something with you.

I am obsessed with Python concurrency, but I wasn't always this way.

My background is in Artificial Intelligence and I have a few fancy degrees and past job titles to prove it.

You can see my LinkedIn profile here:

So what?

Well, AI and machine learning has been hot for the last decade and I have spent that time as a Python machine learning developer:

  • ... working on a range of predictive modeling projects.
  • ... writing more than 1,000+ tutorials.
  • ... and authoring over 20+ books.

There's one thing about machine learning in Python, your code must be fast.

Really fast.

Modeling code is already generally fast, built on top of C and Fortran code libraries.

But you know how it is on real projects...

You always have to glue bits together, wrap the fast code and run it many times, and so on.

Making code run fast requires Python concurrency and I have spent most of the last decade using all the different types of Python concurrency available.

Including threading, multiprocessing, asyncio, and the suite of popular libraries.

I know my way around Python concurrency and I am deeply frustrated at the bad wrap it has.

This is why I started SuperFastPython.com where I share hundreds of free tutorials on Python concurrency.

And this is why I wrote this book...

Introducing:

"Python ThreadPoolExecutor Jump-Start"

A new Ebook designed to teach you thread pools in Python, super fast!

Python ThreadPoolExecutor Jump-Start

You don't want to read a book or take a course.

You want faster Python code, and you want it yesterday.

I hear you.

This is not a textbook on Python Concurrency.

It's not a technical thesis on the internals of process-based concurrency in Python.

Instead, it's what you actually need.

It's a rapid-paced, 7-part ebook to get you started and get you good at using the ThreadPoolExecutor class, super fast.

Each of the 7 lessons was carefully designed to teach one critical aspect of the ThreadPoolExecutor class, with explanations, code snippets and worked examples.

Each lesson ends with an exercise for you to complete to confirm you understood the topic, a summary of what was learned, and links for further reading if you want to go deeper.

Next, let's take a closer look at the 7-lessons in the book.

So What Are The Lessons?
...7 lessons to be completed over 7 days

This book is designed to bring you up-to-speed with how to use the ThreadPoolExecutor class as fast as possible.

As such, it is not exhaustive.

There are many topics that are interesting or helpful, that are not on the critical path to getting you productive.

This book is divided into 7 lessons, they are:

  • Lesson 01: Threads, Executors, and Thread Pools
  • Lesson 02: Configure the ThreadPoolExecutor
  • Lesson 03: Execute Multiple Tasks Concurrently
  • Lesson 04: Execute One-Off Tasks Asynchronously
  • Lesson 05: Query Asynchronous Tasks
  • Lesson 06: Manage Collections of Asynchronous Tasks
  • Lesson 07: Case Study Checking the Status of Websites

Next, let's look at the structure of each lesson.

So What is the Structure of Each Lesson?
...tutorials followed by exercises

Each lesson has two main parts, they are:

  1. The body of the lesson (code tutorial)
  2. The lesson review (exercises and references)

The body of the lesson will introduce a topic with code examples, whereas the lesson review will review what was learned with exercises and links for further information.

Each lesson has a specific learning outcome and is designed to be completed in 10-to-20 minutes.

Each lesson is also designed to be self-contained so that you can read the lessons out of order if you choose, such as dipping into topics in the future to solve specific programming problems.

We Python developers learn best from real and working code examples. So each lesson has multiple large worked examples with sample output.

So, after completing the book, what will you know?

What You Will Know After Reading

...become a "Dev Who Can Write Multithreaded Programs"

This book will transform you from a Python developer into a Python developer that can confidently bring concurrency to your projects with the ThreadPoolExecutor class.

After working through all of the lessons in this book, you will know:

  • How to create a ThreadPoolExecutor to execute IO-bound tasks concurrently.
  • How to configure the ThreadPoolExecutor including inspecting the default configuration, how to set the number of worker threads, thread names, and how to initialize worker threads.
  • How to execute multiple tasks concurrently using the map() method.
  • How to issue asynchronous tasks to the ThreadPoolExecutor using the submit() method.
  • How to query, get results, handle exceptions and use callback functions with Future objects for asynchronous tasks.
  • How to manage collections of asynchronous tasks issued to the ThreadPoolExecutor including how to handle results in task completion order and wait for all tasks or for the first task to complete.
  • How to build upon what you have learned to speed-up the IO-bound task of checking website statuses one-by-one so that the websites are checked concurrently more than 5x faster.

How Long Will It Take To Finish?
...a week of one lesson per day

The book will take about 20-to-30 minutes per lesson to finish, so a few hours total.

But that's too fast.

Instead, I recommend 1 lesson per day, over 7 days (1 week).

Work at your own pace

There's no rush and I recommend that you take your time.

The book is designed to be read linearly from start to finish, guiding you from being a Python developer at the start of the book to being a Python developer that can confidently use the ThreadPoolExecutor class in your project by the end of the book.

In order to avoid overload, I recommend completing one or two lessons per day, such as in the evening or during your lunch break. This will allow you to complete the transformation in about one week.

I recommend you maintain a directory with all of the code you type from the lessons in the book. This will allow you to use the directory as your own private code library, allowing you to copy-paste code into your projects in the future.

I recommend trying to adapt and extend the examples in the lessons. Play with them. Break them. This will help you learn more about how the API works and why we follow specific usage patterns.

What Format Is the Ebook?
(pdf and epub)

The ebook is provide in 2 formats:

  • PDF (.pdf): perfect for reading on the screen or tablet.
  • EPub (.epub): perfect for reading on a tablet with a Kindle or iBooks app.

Who Is This Book For?

Before you pull the trigger, let's make sure it is a good fit for you.

This book is designed for Python developers who want to discover how to use and get the most out of the ThreadPoolExecutor class to write fast programs.

Specifically, this book is for:

  • Developers that can write simple Python programs.
  • Developers that need better performance from current or future Python programs.
  • Developers that are working with IO-bound tasks.

This book does not require that you are an expert in the Python programming language or concurrency.

Specifically:

  • You do not need to be an expert Python developer.
  • You do not need to be an expert in concurrency.

What Version of Python is Used?

All code examples use Python 3.

Python 3.9+ to be exact.

Python 2.7 is not supported because it reached end of life in 2020.

Are There Code Examples?

Yes.

There are 28 .py files.

Each lesson has one or more complete, standalone, and fully-working code examples.

The book is provided in a .zip file that includes a src/ directory containing all source code files used in the book.

How Many Pages Is The Book?

99 pages

The PDF has 99 US letter size pages.

Can I Print The Book?

Yes.

Although, I think it's better working through it on the screen.

  • You can search, skip, and jump around really fast.
  • You can copy-and-paste code examples.
  • You can compare code output directly.

Is There Digital Rights Management (DRM)?

No.

What if I Need Help?

The lessons were designed to be easy to read and follow.

Nevertheless, sometimes we need a little extra help.

A list of further reading resources is provided at the end of each lesson. These can be helpful if you are interested in learning more about the topic covered, such as fine grained details of the standard library and API functions used.

The conclusions at the end of the book provide a complete list of websites and books that can help if you want to learn more about Python concurrency and the relevant parts of the Python standard library. It also lists places where you can go online and ask questions about Python concurrency.

Finally, if you ever have questions about the lessons or code in this book, you can contact me any time and I will do my best to help. My contact details are provided at the end of the book.

Do I Get Free Updates?

Yes.

Each time I release an updated version, I will send you an email with a link so that you can download the latest version for free.

Happiness Guarantee

I want you to be happy, and I stand behind my materials.

If you decide that Python concurrency is not for you, or whatever reason, I'll understand.

I offer a 100% money back guarantee, no questions asked.

Get in touch at:

Can I Buy The Book Elsewhere?

Yes!

You can get a kindle or paperback version from Amazon.

Is There a Paperback Version?

Yes!

You can get a paperback version from Amazon.

Can I Read a Sample?

Yes.

You can read a book sample via google books "preview" or via the amazon "look inside" feature:

Generally, if you like my writing style on SuperFastPython, then you will like the books.

Can I Download the Source Code Now?

The source code (.py) files are included in the .zip with the book.

Nevertheless, you can also download all of the code from the dedicated GitHub Project:

Does Python Concurrency Work on My OS?

Yes.

Python parallelism is built into the Python programming language and works equally well on:

  • Windows
  • MacOS
  • Linux

Does Python Concurrency Work on Hardware?

Yes.

Python parallelism is agnostic to the underlying CPU hardware.

If you are running Python on a modern computer, then you will have support for parallelism, e.g. Intel, AMD, ARM, and Apple Silicon CPUs are supported.

How do the Jump-Start Books Compare to the Guides?

The SuperFastPython Jump-Start books are laser-focused on making you productive with a Python concurrency module or class as fast as possible.

This means that many broader topics are not covered because they are not on the critical path.

The guides on SuperFastPython.com are broader in scope and compare the class or module to related modules, describing best practices, common errors, common usage questions, and common objects.

This material may be interesting but is a distraction when you are focused on getting productive as fast as possible.

Another important difference is that the jump-start books are provided in book form, whereas the guides are very long web pages.

This makes the books easy to read on a kindle, tablet, or paperback, as well as the screen, whereas the guides must be read in the browser.

Any Questions?

Contact me directly, any time about this book or Python concurrency generally.

I'm here to help as best I can.

You can send an email directly to my inbox via:

Praise for Super Fast Python

What Are You Waiting For?

Stop reading out-dated StackOverflow answers.

Learn Python concurrency correctly, step-by-step.

Start today.

Buy now and get your copy in seconds!

I want this!
Formats
PDF and EPUB
Code
28 .py files
Lessons
7
Pages
99
Powered by