Threads in Python

The basics.

Keno Leon

--

Photo by SkitterPhoto on Pexels

Like many things in programming, threads or threading is both easily understandable and daunting( even scary ), sort of like dating. This is my attempt at making threads less scary and easier to understand, it was inspired by my own difficulties learning to use them, hope this helps you.

What’s a thread ?

Conceptually, threading is a way of telling your code to do multiple things at the same time, importantly, there is a need for order and intercommunication, there is also a specific syntax and a few caveats; an individual thread can then be understood as an independent ( although it can talk to other threads ) and concurrent ( other things are happening ) process.

In Practice

There are many instances where threading can help you solve a problem, in my specific case it was a pesky GUI that needed to call other functions and was hanging ( freezing ) my program, threads solved the issue. Rather than recreate that complex problem here, I opted for analogies and simple scripts that will hopefully help you get the gist and then you can use threading for your specific projects.

The Initial Problem

It’s not uncommon to want 2 things to happen at the same time, let’s start with an example that doesn’t quite work:

--

--