python多进程
多进程
from multiprocessing import Process
p = Process(target=func, args=('test',))
p.start()
p.join()
案例实现:
from multiprocessing import Process
def run_proc(name):
print('Run child process %s (%s)...' % (name, os.getpid()))
if __name__=='__main__':
p = Process(target=run_proc, args=('test',))
p.start()
p.join()
进程安全
import multiprocessing
lock = multiprocessing.Lock()
with lock:
# do something
进程通信
from multiprocessing import Queue
q = Queue()
q.put(value)
value = q.get()
案例实现:
from multiprocessing import Process, Queue
import os, time, random
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__':
q = Queue()
pw = Process(target=write, args=(q,))
pr = Process(target=read, args=(q,))
pw.start()
pr.start()
pw.join()
pr.terminate()
进程池技术
from concurrent.futures import ProcessPoolExecutor
with ProcessPoolExecutor() as pool:
# 方法一
result = pool.map(func, ["test", ])
# 方法二
future = pool.submit(func, ["test", ])
result = future.result()
案例实现:
import math
from concurrent.futures import ProcessPoolExecutor
import time
PRIMES = [112272535095293] * 100
def is_prime(n):
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True
def multi_process():
with ProcessPoolExecutor() as pool:
pool.map(is_prime, PRIMES)
if __name__ == "__main__":
start = time.time()
multi_process()
end = time.time()
print("multi_process, cost:", end - start, "seconds")