Threads vs. Processes on Linux
Learn how to create multithreaded processes in Python.
We'll cover the following
In this lesson, we’ll create multithreaded processes with a focus on Linux, as Python is closely tied with Linux development. The theory is the same for Windows as well, although the underlying implementation differs.
Processes
Any program that runs in Linux is a process. Each process has its memory, stack, and access to shared peripherals. As far as each process is concerned, it has the whole computer to itself. Additionally, if you’ve done any Windows programming (programming the Microsoft Windows API), you’ll find some differences in terminology. Microsoft has a few special terms it uses for its processes.
So how do multiple processes run at the same time? That’s the job of the operating system. It ensures that each process can only access its memory, run on the CPU, access external peripherals like the network, and so on. Processes are considered heavy-duty as each starts with a complete execution environment. To get around this, sometimes programmers use threads.
Threads
A thread is a lightweight process. They are smaller units of a running process. Which means they share the memory and other resources of the process that started them. If we have a complicated algorithm, we can break it into threads and run it in parallel. Since all the threads can see a shared memory, they can work together without a complex messaging mechanism. Isn’t that great!
Threads can get messy and lead to horrible bugs. These bugs might occur for many reasons. One such reason could be timing issues (does thread A run before thread B?). Another could be race conditions, where two threads “race” to update the same code or memory. This leads to unpredictable behavior. Locks, semaphores, and the like were invented to get around these issues. Going any deeper is beyond the scope of this lesson in this course.
Python doesn’t have built-in thread support. Instead, it uses the thread of its operating system (Pthreads on Linux or Windows threads on Windows).
But let’s say we find a dream problem that is truly independent. There is no data sharing, so no risk of race conditions or timing problems. Such a thing is possible in complex mathematical problems. What then? In that case, threads should always be used.
Get hands-on with 1400+ tech skills courses.