Difference between subprocess and thread Difference between subprocess and thread multithreading multithreading

Difference between subprocess and thread


First, we have to understand what actually the Process is, then Thread, if we could do that, then answer of this question will be easier.

Process vs. Thread

A process (also sometimes referred to as a task) is an executing (i.e., running) instance of a program. Threads are lightweight processes that can run in parallel and share an address space (i.e., a range of memory locations) and other resources with their parent processes (i.e., the processes that created them).

Above part is taken from this link: http://www.linfo.org/context_switch.html

A process is a program in execution. For example, when we write a program in C or C++ and compile it, the compiler creates a binary code. The original code and Binary code, both are programs. When we actually run the binary code, it becomes a process.A process is an ‘active’ entity as opposed to a program which is considered to be a ‘passive’ entity. A single program can create many processes when run multiple times, for example when we open a .exe or binary file multiple times, many instances begin (many processes are created).

Above part is taken from this link: https://www.geeksforgeeks.org/gate-notes-operating-system-process-management-introduction/

A process is an instance of program (e.g. Jupyter notebook, Python interpreter). Processes spawn threads (sub-processes) to handle subtasks like reading keystrokes, loading HTML pages, saving files. Threads live inside processes and share the same memory space.

Example: Microsoft WordWhen you open Word, you create a process. When you start typing, the process spawns threads: one to read keystrokes, another to display text, one to autosave your file, and yet another to highlight spelling mistakes. By spawning multiple threads, Microsoft takes advantage of idle CPU time (waiting for keystrokes or files to load) and makes you more productive.

Above part is taken from this link: https://medium.com/@bfortuner/python-multithreading-vs-multiprocessing-73072ce5600b

Python provide thread and subprocess library for the programmer for multitasking. The Purpose of both almost same but what we will choose thread or subproces depends on what we want. Threading maybe good enough for IO bound app, but not CPU bound app because it uses only SINGLE CPU core at once, because of Python's GIL and The failure of one thread would crash the whole app.

Subprocess is Good for both IO bound app and CPU bound app, but consuming more memory. Could use all the CPU core at the same time (parallel) and the failure of single process would not affect the others.

Above part is taken from this link: https://www.quora.com/For-multiprocessing-in-Python-which-library-should-I-use-threading-or-subprocess