From what I understand, thread and process differs in that thread shares resources.
I am not sure if this differs from one language to another, but how do you differentiate between thread and process in general or in Python?
Are every independent functions different process? Would a class methods be threads as they share memory? would recursive functions be thread?
If threading or processing module is not used, how can it be differentiated? and IS there a need to differentiate them?
Thank you
Advertisement
Answer
I’ll answer your questions directly:
From what I understand, thread and process differs in that thread shares resources.
This is not true, at least not from your understanding. Consider that you can edit a file in Notepad
and in WordPad
at the same time; both are different processes sharing the same resource; a file in this case.
Conceptually, a process is a single “application” that shares the resources of the operating system and other processes while a thread is a single “execution context” that shares the same basic resources of a process.
It’s a little more complicated than that, but at a high level that’s the basic difference between a process and a thread.
I am not sure if this differs from one language to another, but how do you differentiate between thread and process in general or in Python?
Given the prior point, in a general basic context, a thread is owned by a process, and every process has at least one thread.
From a code perspective, you can differentiate by getting the process ID and thread ID, and that does depend on your langauge.
For Python, you would import os
and then call os.getpid()
to get the current process ID, and if you’re using Python 3.8 you can call threading.get_native_id()
to get the thread id from which the function is currently executed on.
If you were to create 10 threads within the same process, each thread should return the same value for os.getpid()
but should return 10 different ID’s for threading.get_native_id()
.
Additionally, a thread within a process can spawn another thread or separate process. For example, a single process (read: application) could create 10 threads, and in each thread could spawn an external application (read: process), but that process is not owned by the thread that spawned it like the thread is owned by the process that spawned it.
Another basic way to think of it, all processes are children of the OS, and all threads are children of the parent process, no matter which thread or process created what.
Are every independent functions different process? Would a class methods be threads as they share memory? would recursive functions be thread?
No. Each function lives within the same process memory space, including classes and their member functions. Recursive functions are neither processes nor threads, they are functions that call themselves.
You could create a thread that calls a specific function, and that function could potentially be recursive, but again, you have to have your code specifically create a thread and utilize the function address which lives within the process created when your init
code was called (main
in other languages).
If threading or processing module is not used, how can it be differentiated?
Referring again to the previous points, you differentiate it by understanding what a process is and what a thread is.
and IS there a need to differentiate them?
Yes. Conceptually they are different things, and once you actually start doing various types of multi-threaded, multi-process, memory-mapped code, you do need to understand that difference and how it pertains to your code.
At a bare minimum, if you never write a single line of multi-threaded code in your career, it is important to at least understand how they relate.