What’s the difference between a process and a thread?

Let’s break it down, starting with process.
It starts with a program you write, like a Python script. It lives somewhere on the disk.

When you run it, it’s loaded into the memory.
The program becomes a process.

Process is like a factory.
It includes the code and the system resources required to run the program.

Inside every process, there are threads.
Threads are like workers.
Every one of them can run code independently.

If you do a simple task like printing hello world, you probably only need one main thread.
But if you do more IO bound tasks like network requests, you will need more threads to make it more efficient.
Now here's the key difference between processes and threads. Threads share the same resources:

while each process has its own memory space and resources:

If you aren’t careful, different threads will compete for the same resource and get stuck.

This is called a deadlock, which looks something like this:

Fee from Anime Coders
PS: Want us to make a personalised lecture for you? Share with us what you want to learn here.
