2.5. 进程线程

多任务实现类型: - 多进程模式; - 多线程模式; - 多进程+多线程模式。

2.5.1. fork

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os 
print("process {} start ".format(os.getpid()))

pid = os.fork()
if pid ==0 : 
    print("i am child ,my pid={},ppid={}".format(os.getpid(),os.getppid()))
else:
    print("i am parent,my pid {}, my child pid {}".format( os.getpid(),pid))

2.5.2. multiprocess

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os 
from multiprocessing import Process

def f(name):
    print("name")
p = Process(target=f,args=("test",))
print("process start")
p.start()
p.join()
print("process end ")

2.5.3. pool

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os 
from multiprocessing import Process,Pool
import time 
import random 

def my_task(name):
    time.sleep(random.random()*3)
    print(f"{name} end")

p = Pool(3)
for i in range(5):
    p.apply_async(my_task,args=(i,))
p.close()
p.join()
print("all done")

2.5.4. subprocess

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess

r = subprocess.call(["nslookup","www.python.org"])
print(r)

p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b'python.org\nexit\n')
print(output.decode('utf-8'))
print(p.returncode)

2.5.5. ipc

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
from multiprocessing import Process, Queue
import os, time, random

def write(q): 
    for value in ['a','b','c']: 
        q.put(value)
        time.sleep(random.random())

def read(q): 
    while True: 
        value = q.get(True)
        print(value)

q = Queue()
pw = Process(target=write,args=(q,))
pr = Process(target=read,args=(q,))

pw.start()
pr.start()
pw.join()
pr.terminate()

2.5.6. 多线程

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time 
import threading
import multiprocessing


balance =0 
lock = threading.Lock()

def change_balance(n): 
    global balance 
    balance = balance + n 
    balance = balance - n 

def run_thread(n): 
    for i in range(20000000 * multiprocessing.cpu_count()):
         lock.acquire()
         try: 
            change_balance(i )
         finally: 
             lock.release()


t1 = threading.Thread(target=run_thread,args=(5,))
t2 = threading.Thread(target=run_thread,args=(58,))
t3  = threading.Thread(target=run_thread,args=(58,))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join() 
t3.join()

print(balance)

一个ThreadLocal变量虽然是全局变量,但每个线程都只能读写自己线程的独立副本,互不干扰。ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import threading 

local_school = threading.local

def process_student():
    stu = local_school.student 
    print("{} {}",threading.current_thread().name,stu)

def process_thread(name):
    local_school.student = name 
    process_student()

t1 = threading.Thread(target=process_thread,args=("zhao",),name="thread-zhao")
t2= threading.Thread(target=process_thread,args=("qian",),name="thread-qian")
t1.start()
t2.start()
t1.join()
t2.join()

2.5.7. 分布式进程

通过分布式进程可以将任务分布到多台机器上面去。 通过网络完成信息通信。