In the last tutorial, we introduced you to line_profiler, a package that can help you time profile your code. It is now time to take a step forward.

In this tutorial, we will learn about implementing multi-threading and multi-processing approaches using Python. These approaches guide the operating system to make optimum utilization of one’s system hardware and hence make the code execution efficient.

Let’s get started…

Multi-threading

Quoting Wiki— In computer architecture, multi-threading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system.

Given concurrency, one can initiate parallel execution of multiple processes and achieve faster runtime. Without going into the technical details (google and read about GIL), one thing to keep in mind is that multi-threading is more efficient when performing I/O based tasks like downloading images and files. On the other hand, multi-processing is better suited for CPU based tasks that are computationally intensive.

Multi-threading implementation in Python

To implement multi-threading, we will be using Python’s standard library,** threading**. The library comes by default with standard Python installation and hence can be imported directly in our code.

For demonstrating the effectiveness of multi-threading, we will be downloading 5 images from Unsplash. Let us observe the execution time when we download these images sequentially:

#programming #python-programming #machine-learning #software-engineering #data-science

How To Make Your Python Code Run Faster — 1st Installment
1.25 GEEK