廖雪峰python学习笔记14.多进程和多线程

来源:互联网 发布:js 回调函数 执行顺序 编辑:程序博客网 时间:2024/04/28 21:58
# -*- coding :utf-8  -*-
#多进程
#Python中的多进程和c语言中比较相似,都是以调用fork()函数展开的
#fork()函数:子进程永远返回0,而父进程返回子进程的ID
#进程调用getpid()函数进程的ID
#子进程调用getppid()得到父进程的ID


import os
print('Processing (%s) start...' % os.getpid())
pid = os.fork()
if pid == 0:
    print('Hey i am child process (%s), my parent process is %s '%(os.getpid(), os.getppid()))
else:
    print('I (%s) just create a processing %s' %(os.getpid(), pid))
    
#进程池pool(并发多进程)
"""
Pool可以提供指定数量的进程,供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;
但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程来它。这里有一个简单的例子:
"""
from multiprocessing import Pool
from time import sleep


def f(x):
    sleep(300)
    print('Hey I am process %d' %i)
    


pool = Pool(3) #设定进程池容量为3
for i in range(3):
    result = pool.apply_async(f,(i)) #pool.apply_async()用来向进程池提交目标请求
pool.close()
pool.join()#pool.join()是用来等待进程池中的worker进程执行完毕
if result.successful():
    print ('successful')


#进程间通讯,以queue为例
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__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    # pr进程里是死循环,无法等待其结束,只能强行终止:
    pr.terminate()


#多线程和linux c类似,不必再多说,贴一段代码即可
    
import time, threading


# 假定这是你的银行存款:
balance = 0


def change_it(n):
    # 先存后取,结果应该为0:
    global balance
    balance = balance + n
    balance = balance - n


def run_thread(n):
    for i in range(100000):
        change_it(n)


t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)


#进程锁
balance = 0
lock = threading.Lock()


def run_thread(n):
    for i in range(100000):
        # 先要获取锁,获得了锁,因此其他线程不能同时执行change_it(),只能等待
        lock.acquire()
        try:
            # 放心地改吧:
            change_it(n)
        finally:
            # 改完了一定要释放锁:
            lock.release()


#注意
#Python不能利用多线程实现多核任务
0 0
原创粉丝点击