1 多进程(multiprocessing)
1.1 Unix/Linux操作系统
import os
print('process (%s) start....' % os.getpid())
pid = os.fork() # 返回子进程的ID
if pid == 0:
print('i am child process (%s) and my parent is (%s).' % (os.getpid(), os.getppid()))
else:
print('i (%s) just created a child process (%s)' % (os.getpid(), pid))
# 运行结果:
process (12170) start....
i (12170) just created a child process (12171)
i am child process (12171) and my parent is (12170).
注意:
- fork() 函数,调用一次,返回两次。因为操作系统自动把当前进程(父进程)复制了一份(子进程),然后分别在父进程和子进程内返回。
- 子进程返回0,父进程返回子进程的ID。子进程通过getppid()方法可以获取父进程的ID。
- Windows系统上不能使用fork()。
1.2 Windows操作系统
# multiprocessing是跨平台的多进程模块
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.')
1.3 Pool(进程池)
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) # pool默认值为CPU核数
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print('waiting for all subprocesses done...')
p.close() # 调用close()后就不能继续添加process了
p.join() # 调用join()之前要调用close()
print('all subprocesses done.')
1.4 进程间通信
from multiprocessing import Process,Queue
import os,time,random
'在父进程中创建两个子进程,一个向queue中写数据,一个读数据'
# 写数据进程代码
def write(q):
print('process to write: %s' %os.getpid())
for value in ['a','b','c']:
print('put %s to queue...' % value)
q.put(value)
time.sleep(random.random())
# 读数据进程代码
def read(q):
print('process to read: %s ' % os.getpid())
while True:
value=q.get(True)
print('get %s from queue' % value)
if __name__ =='__main__':
# 1 父进程创建queue,并传给各个子进程
q=Queue()
pw=Process(target=write,args=(q,))
pr=Process(target=read,args=(q,))
# 2 启动子进程pw,写入数据
pw.start()
# 3 启动子进程pr,读取数据
pr.start()
# 4 等待pw结束
pw.join()
# 5 pr进程是死循环,需要强行终止
pr.terminate()
运行结果:
process to write: 12661
put a to queue...
process to read: 12662
get a from queue
put b to queue...
get b from queue
put c to queue...
get c from queue
2 多线程
2.1 创建线程并运行
import time, threading
# 线程执行代码
def loop():
print('thread %s is running...' % threading.current_thread().name)
n = 0
while n < 5:
n = n + 1
print('thread %s >>> %s' % (threading.current_thread().name, n))
time.sleep(1)
print('thread %s ended.' % threading.current_thread().name)
print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
t.join()
print('thread %s ended.' % threading.current_thread().name)
运行结果:
thread MainThread is running...
thread LoopThread is running...
thread LoopThread >>> 1
thread LoopThread >>> 2
thread LoopThread >>> 3
thread LoopThread >>> 4
thread LoopThread >>> 5
thread LoopThread ended.
thread MainThread ended.
2.2 Lock(锁)
import threading
balance = 0
def change_it(n):
global balance
balance = balance + n
balance = balance - n
# 创建一个锁
lock = threading.Lock()
def run_thread(n):
for i in range(100):
# 获取锁
lock.acquire()
try:
change_it(n)
finally:
# 释放锁
lock.release()
t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)
注意:
- 获得锁的线程用完后一定要释放锁,否则会成为死线程。
- 锁的好处:确保某段关键代码只能由一个线程从头到尾完整地执行。
- 锁的坏处:阻止了多线程并发执行,效率低;存在多个锁时,若有嵌套,可能会有死锁发生。
- Python解释器执行代码时,有一个GIL锁:Global Interpreter Lock,任何Python线程在执行前,都必须后去GIL锁,没执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行。
- 这个GIL全局锁实际上把所有线程的执行代码都给上了锁,所以,多线程在Python中只能交替执行,即使100个线程跑在100核CPU上,也只能用到1个核。