線程代替方案
- subprocess
- 完全跳過線程,使用進程
- 是派生進行的主要替代方案
- python2.4後進入
- multiprocessiong
- 使用thronging接口派生,使用子進程
- 允許爲多核或者多cpu派生進程,接口跟threading非常相似
- python2.6
- concurrent.futures
- 新的異步執行模塊
- 任務級別的操作
- python3.2後引入
多進程
- 進程間通訊(InterprocessCommunication,IPC)
- 進程之間無任何共享狀態
- 進程的創建
import multiprocessing
from time import sleep, ctime
def clock(interval):
i = 0
while i < 5:
print("The time is %s" % ctime())
sleep(interval)
i += 1
if __name__ == '__main__':
p = multiprocessing.Process(target=clock, args=(5,))
p.start()
The time is Mon Jul 2 14:07:34 2018
The time is Mon Jul 2 14:07:39 2018
The time is Mon Jul 2 14:07:44 2018
The time is Mon Jul 2 14:07:49 2018
The time is Mon Jul 2 14:07:54 2018
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):
i = 0
while i < 5:
print("The time is %s" % ctime())
sleep(self.interval)
i += 1
if __name__ == "__main__":
p = ClockProcess(3)
p.start()
The time is Mon Jul 2 14:08:09 2018
The time is Mon Jul 2 14:08:12 2018
The time is Mon Jul 2 14:08:15 2018
The time is Mon Jul 2 14:08:18 2018
The time is Mon Jul 2 14:08:21 2018
from multiprocessing import Process
import os
def info(title):
print(title)
print("module name:", __name__)
print("parent process:", os.getppid())
print("process id :", os.getpid())
def f(name):
info('function f')
print('hello', name)
if __name__ == '__main__':
info('main line')
p = Process(target=f, args=('whj',))
p.start()
main line
module name: __main__
parent process: 19586
process id : 20428
function f
module name: __main__
parent process: 20428
process id : 20463
hello whj
import multiprocessing
from time import sleep, ctime
def consumer(input_q):
print("Into consumer:", ctime())
i = 0
while i < 100:
item = input_q.get()
print("pull", item, "out of q")
input_q.task_done()
i += 1
print("Out of consumer:", ctime())
def producer(sequence, output_q):
print("Into producer:", ctime())
for item in sequence:
output_q.put(item)
print("put", item, "into q")
print("Out of producer:", ctime())
if __name__ == "__main__":
q = multiprocessing.JoinableQueue()
qc = multiprocessing.Process(target=consumer, args=(q,))
qc.daemon = True
qc.start()
sequence = [1, 2, 3, 4]
producer(sequence, q)
q.join()
import multiprocessing
from time import sleep, ctime
def consumer(input_q):
print("Into consumer:", ctime())
while True:
item = input_q.get()
if item is None:
break
print("pull", item, "out of q")
input_q.task_done()
print("Out of consumer:", ctime())
def producer(sequence, output_q):
print("Into producer:", ctime())
for item in sequence:
output_q.put(item)
print("put", item, "into q")
print("Out of producer:", ctime())
if __name__ == "__main__":
q = multiprocessing.JoinableQueue()
qc = multiprocessing.Process(target=consumer, args=(q,))
qc.daemon = True
qc.start()
sequence = [1, 2, 3, 4, -1]
producer(sequence, q)
q.put(None)
qc.join()