Multithreading or Multiprocessing?
Abstract: Hi, I'm back. Do you know, when you open a new tag, chrome will create a process, of course, may the process will run by many threads. But why chrome not using one process and many threads mode that when you open a tag, it creates a thread?
First of all, I wanna say most of us computer are multiprocessing and multithreading. We're not going to talk about that.
What's Multiprocessing?
The "multi" in multiprocessing refers to the multiple cores in a computer's central processing unit. Just in case, One processor may sit idle, and the other may be overloaded with the specific processes, then multiprocessing came over.
In a true multi-core processor, each of the cores is functionally equivalent to an individual CPU. They may be fabricated on the same silicon die, and there are normally some shared components such as clock generators, power control circuits, and higher-level caches but… basically each core is a separate processor which can work independently of the others. That means each core has its own registers, its own ALU, and its own copy of all the other parts of the execution pipeline.
So-called “multithreading” CPU cores are a little bit more varied in their designs. The basic idea is that a “multi-threaded” core contains multiple copies of some—but not all—of the hardware necessary to execute a program. The extra hardware allows the CPU core to execute multiple threads or processes more efficiently, but without the expense of adding a whole extra CPU core. In all of the real-world implementations that I’ve heard of (including Intel Hyper-threading™), there is a separate set of general-purpose registers for each thread. This makes context switches faster, because the core can switch between executing multiple threads without saving all of the registers to memory.
What's multithreading?
Multithreading is a program execution technique that allows a single process to have multiple code segments (like threads). It also runs concurrently within the "context" of that process. Multi-threaded applications are applications that have two or more threads that run concurrently. Therefore, it is also known as concurrency.
Copied from https://www.guru99.com/difference-between-multiprocessing-and-multithreading.html
contrast
parameter | multiprocessing | multithreading |
---|---|---|
basic | increase computing power | create computing threads of a single process to increase computing power |
execution | it allows you to execute multiple processes concurrently | multiple threads of a single process are executed concurrently |
CPU switching | In Multiprocessing, CPU has to switch between multiple programs so that it looks like that multiple programs are running simultaneously | In multithreading, CPU has to switch between multiple threads to make it appear that all threads are running simultaneously |
Creation | The creation of a process is slow and resource-specific | The creation of a thread is economical in time and resource |
Memory | Multiprocessing allocates separate memory and resources for each process or program | Multithreading threads belonging to the same process share the same memory and resources as that of the process |
the last one, why chrome choose multiprocessing
more security. Shared memory can lead more probability to appear bug, and if a part of the crash, the program will crash.
https://www.zhihu.com/question/368712837
multiprocessing python instance on windows platform
from multiprocessing import Process
import os
def run_proc(name):
print('Run child process %s (%s)' % (name, os.getpid()))
if __name__=='__main__':
print('Parent process %s.' % os.getpid())
p = Process(target=run_proc, args=('test',))
print('Child process will start.')
p.start()
p.join()
print('Child process end.')
Pool(start large subprocesses, you may need it)
from multiprocessing import Pool
import os, time, random
def long_time_task(name):
print('Run task %s (%s)...' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print('Task %s runs %0.2f seconds.' % (name, (end - start)))
if __name__=='__main__':
print('Parent process %s.' % os.getpid())
p = Pool(4)
for i in range(11):
p.apply_async(long_time_task, args=(i,))
print('Waiting for all subprocesses done...')
p.close()
p.join()
print('All subprocesses done.')
here is the result, due to pool(4), it means allow four process running at the same time.
Parent process 21432.
Waiting for all subprocesses done...
Run task 0 (19768)...
Run task 1 (21756)...
Run task 2 (5852)...
Run task 3 (3976)...
Task 3 runs 0.05 seconds.
Run task 4 (3976)...
Task 1 runs 1.00 seconds.
Run task 5 (21756)...
Task 4 runs 1.34 seconds.
Run task 6 (3976)...
Task 0 runs 1.71 seconds.
Run task 7 (19768)...
Task 7 runs 0.09 seconds.
Run task 8 (19768)...
Task 2 runs 1.86 seconds.
Run task 9 (5852)...
Task 6 runs 1.12 seconds.
Run task 10 (3976)...
Task 8 runs 0.98 seconds.
Task 5 runs 2.32 seconds.
Task 9 runs 2.53 seconds.
Task 10 runs 2.33 seconds.
All subprocesses done.
subprocess
Mostly, the subprocess is an external process rather than itself.
import subprocess
print('$ nslookup www.python.org')
r = subprocess.call(['nslookup', 'www.python.org'])
print('Exit code:', r)