1、线程代替方案
-subprocess
-完全跳过线程,使用进程
-是派生进程的主要替代方案
-python2.4后引入
-multiprocessing
-使用threading借口派生,使用子进程
-允许为多核或者多cpu派生进程,接口跟theading非常相似
-python2.6
-concurrent.futures
-新的异步执行模块
-任务级别的操作
-python3.2后引入
2、多进程
-进程间通讯(InterprocessCommunication,IPC)
-进程之间无任何共享状态
-进程的创建
-直接生成Process实例对象
import multiprocessing
from time import sleep,ctime
def clock(interval):
while True:
print("The time is %s" % ctime())
sleep(interval)
if __name__ == '__main__':
p = multiprocessing.Process(target=clock,args=(5,))
p.start()
while True:
print("sleeping.....")
sleep(1)
运行:
sleeping.....
sleeping.....
sleeping.....
The time is Mon Apr 1 21:06:44 2019
sleeping.....
sleeping.....
sleeping.....
sleeping.....
The time is Mon Apr 1 21:06:49 2019
sleeping.....
sleeping.....
sleeping.....
sleeping.....
sleeping.....
The time is Mon Apr 1 21:06:54 2019
sleeping.....
-派生子类
import multiprocessing
from time import sleep,ctime
class ClockProcess(multiprocessing.Process):
'''
两个函数比较重要
1、init构造函数
2、run
'''
def __init__(self,interval):
super().__init__()
self.interval = interval
def run(self):
while True:
print("The time is %s" % ctime())
sleep(self.interval)
if __name__ == '__main__':
p = ClockProcess(3)
p.start()
while True:
print("sleeping....")
sleep(1)
运行:
sleeping....
The time is Mon Apr 1 21:31:14 2019
sleeping....
sleeping....
sleeping....
The time is Mon Apr 1 21:31:17 2019
sleeping....
-在os中查看pid,ppid以及他们的关系
from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:',__name__)
#得到父亲进程的id
print('parent process:',os.getppid())
#得到本身进程的id
print('process id :',os.getppid())
def f(name):
info('function f')
print('hello',name)
if __name__ == '__main__':
info('main line')
p = Process(target=f,args=('bob',))
p.start()
运行:
main line
module name: __main__
parent process: 2796
process id : 2796
function f
module name: __mp_main__
parent process: 6544
process id : 6544
hello bob
-生产者消费者模型
-JoinableQueue
-设置哨兵
import multiprocessing
from time import ctime
#设置哨兵问题
def consumer(input_q):
print("Into consumer:",ctime())
while True:
#处理项
item = input_q.get()
if item is None:
break
#此处替换为有用的工作
print("pull",item,'oout of q')
#发出信号通知任务完成
# input_q.task_done()
#此句未执行,因为q.join()收集到四个task_done()信号后,主进程启动,未等到print此句
print("Out of consumer:",ctime())
def producer(sequence,output_q):
for item in sequence:
print("Into procuder:", ctime())
output_q.put(item)
print("put",item,"into q")
print("Out of procuder:",ctime())
if __name__ == '__main__':
#做完了通知发个消息~
q = multiprocessing.JoinableQueue()
#运行消费者进程
cons_p1 = multiprocessing.Process(target=consumer,args=(q,))
# cons_p.daemon = True
cons_p1.start()
cons_p2 = multiprocessing.Process(target=consumer, args=(q,))
cons_p2.start()
#生产多个项,sequence代表要发送给消费者的项xulie
#在实践中,这可能是生成器的输出或通过一些其他方式生产出来
sequence = [1,2,3,4]
producer(sequence,q)
q.put(None)
q.put(None)
#等待所有项被处理
cons_p1.join()
cons_p2.join()
运行:
Into procuder: Mon Apr 1 22:28:28 2019
put 1 into q
Into procuder: Mon Apr 1 22:28:28 2019
put 2 into q
Into procuder: Mon Apr 1 22:28:28 2019
put 3 into q
Into procuder: Mon Apr 1 22:28:28 2019
put 4 into q
Out of procuder: Mon Apr 1 22:28:28 2019
Into consumer: Mon Apr 1 22:28:28 2019
pull 1 oout of q
pull 2 oout of q
pull 3 oout of q
pull 4 oout of q
Out of consumer: Mon Apr 1 22:28:28 2019
Into consumer: Mon Apr 1 22:28:28 2019
Out of consumer: Mon Apr 1 22:28:28 2019