1.Python是单核的,只能充分利用1个CPU的1个核
2.对一个多线程程序,线程间即使启动多个子解释器,依然有GIL这个全局锁,会引起串行。
串行是libpython导致的。
3.要使用Python做并行计算或规避多线程串行的问题。使用multiprocessing包时最好的选择。
在多进程环境下,每个进程可以运行一个解释器,互相之间是独立的,没有锁。multiprocessing包提供了进程调用实现的封装。包括启动进程,进程间消息队列,进程池等等。
这里举一个简单的例子,可以实现任意函数的IPC或者说RPC封装,实现任意函数在子进程中运行并返回结果给主进程:
multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.
from multiprocessing import Process
from multiprocessing import<