Python 进程
因为GIL的限制,一个进程中只能有一个线程获取到资源,因此在有些场景下会显得很鸡肋。一种解决方法就是利用多进程,通过开多个进程就可以绕开GIL的限制。
多进程
python提供了一个包,multiprocess ,使用方法跟多线程的一样
1  | from multiprocessing import Process  | 
process 0 ----
[0]
process 1 ----
[1]
process 2 ----
[2]
process 3 ----
[3]
process 4 ----
[4]1  | 
  | 
from multiprocessing import Process
from multiprocessing import Array
plist = Array('i',10)
def addProcess(i):
    plist[0] += 200
    print 'process %d ----'%i
    print [item for item in plist]
for i in range(5):
    p = Process(target=addProcess,args=(i,))
    p.start()
process 0 ----
[200, 0, 0, 0, 0, 0, 0, 0, 0, 0]
process 1 ----
[400, 0, 0, 0, 0, 0, 0, 0, 0, 0]
process 2 ----
[600, 0, 0, 0, 0, 0, 0, 0, 0, 0]
process 3 ----
[800, 0, 0, 0, 0, 0, 0, 0, 0, 0]
process 4 ----
[1000, 0, 0, 0, 0, 0, 0, 0, 0, 0]   1  | 
  | 
def Array(typecode_or_type, size_or_initializer, **kwds):
    '''
    Returns a synchronized shared array
    '''
    from multiprocessing.sharedctypes import Array
    return Array(typecode_or_type, size_or_initializer, **kwds)1  | 
  | 
‘c’: ctypes.c_char, ‘u’: ctypes.c_wchar,
‘b’: ctypes.c_byte, ‘B’: ctypes.c_ubyte,
‘h’: ctypes.c_short, ‘H’: ctypes.c_ushort,
‘i’: ctypes.c_int, ‘I’: ctypes.c_uint,
‘l’: ctypes.c_long, ‘L’: ctypes.c_ulong,
‘f’: ctypes.c_float, ‘d’: ctypes.c_double1  | 
  | 
def Manager():
    '''
    Returns a manager associated with a running server process
    The managers methods such as `Lock()`, `Condition()` and `Queue()`
    can be used to create shared objects.
    '''
    from multiprocessing.managers import SyncManager
    m = SyncManager()
    m.start()
    return m1  | 
  | 
# -*- coding:utf-8 -*-
from multiprocessing import Pool
import time
def f1(args):
    time.sleep(1)
    print(args)
if __name__ == '__main__':
    p = Pool(5)
    for i in range(30):
        p.apply_async(func=f1, args= (i,))
    p.close()           # 等子进程执行完毕后关闭进程池
    # time.sleep(2)
    # p.terminate()     # 立刻关闭进程池
    p.join()```
进程池内部维护一个进程序列,当使用时,去进程池中获取一个进程,如果进程池序列中没有可供使用的进程,那么程序就会等待,直到进程池中有可用进程为止。
进程池中有以下几个主要方法:
apply:从进程池里取一个进程并执行
apply_async:apply的异步版本
terminate:立刻关闭进程池
join:主进程等待所有子进程执行完毕。必须在close或terminate之后。
close:等待所有进程结束后,才关闭进程池。