python进程、线程和协程(二)

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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值