Python系统学习第二十六课

线程替代方案

  • subprocess

    • 完全跳过线程,使用进程
    • 是派生进程的主要替代方案
  • multoprocessiong

    • 使用threading接口派生,使用子进程
    • 允许为多核或者多cpu派生进程,接口跟threading非常相似
  • concurrent.futures

    • 新的异步执行操作
    • 任务级别的操作

多进程

  • 进程间通讯(interprocesscommunication,ipc)
  • 进程之间无任何共享状态
  • 进程的创建
    • 直接生成process实例对象
    • 派生子类
  • 在os中查看pid,ppid以及他们的关系
#进程的创建
import multiprocessing
from time import sleep, ctime


def clock(interval):
    while True:
        print("The time is %s" % ctime())
        sleep(interval)



if __name__ == '__main__':
    p = multiprocessing.Process(target = clock, args = (5,))
    p.start()

    while True:
        print('sleeping.......')
        sleep(1)


sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......
sleeping.......



---------------------------------------------------------------------------

KeyboardInterrupt                         Traceback (most recent call last)

<ipython-input-1-40db19ef8149> in <module>()
     16     while True:
     17         print('sleeping.......')
---> 18         sleep(1)


KeyboardInterrupt: 

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):
        while True:
            print("The time is %s" % ctime())
            sleep(self.interval)


if __name__ == '__main__':
    p = ClockProcess(3)
    p.start()

    while True:
        print('sleeping.......')
        sleep(1)

sleeping.......
sleeping.......
sleeping.......



---------------------------------------------------------------------------

KeyboardInterrupt                         Traceback (most recent call last)

<ipython-input-2-2edceb929787> in <module>()
     26     while True:
     27         print('sleeping.......')
---> 28         sleep(1)


KeyboardInterrupt: 
from multiprocessing import Process
import os


def info(title):
    print(title)
    print('module name:', __name__)
    # 得到父亲进程的id
    print('parent process:', os.getppid())
    # 得到本身进程的id
    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=('bob',))
    p.start()
    p.join()
main line
module name: __main__
parent process: 15556
process id: 12148
  • 生产者消费者模型
    • joinableQueue
    • 案例
    • 队列中哨兵的使用
    • 案例
    • 哨兵的改进
    • 案例
import multiprocessing
from time import ctime


def consumer(input_q):
    print("Into consumer:", ctime())
    while True:
        # 处理项
        item = input_q.get()
        print ("pull", item, "out of q") # 此处替换为有用的工作
        input_q.task_done() # 发出信号通知任务完成
    print ("Out of consumer:", ctime()) ##此句未执行,因为q.join()收集到四个task_done()信号后,主进程启动,未等到print此句完成,程序就结束了


def producer(sequence, output_q):
    print ("Into procuder:", ctime())
    for item in sequence:
        output_q.put(item)
        print ("put", item, "into q")
    print ("Out of procuder:", ctime())



# 建立进程
if __name__ == '__main__':
    q = multiprocessing.JoinableQueue()
    # 运行消费者进程
    cons_p = multiprocessing.Process (target = consumer, args = (q,))
    cons_p.daemon = True
    cons_p.start()

    # 生产多个项,sequence代表要发送给消费者的项序列
    # 在实践中,这可能是生成器的输出或通过一些其他方式生产出来
    sequence = [1,2,3,4]
    producer(sequence, q)
    # 等待所有项被处理
    q.join()
Into procuder: Tue Jan  1 17:05:34 2019
put 1 into q
put 2 into q
put 3 into q
put 4 into q
Out of procuder: Tue Jan  1 17:05:34 2019
import multiprocessing
from time import 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")
    print ("Out of consumer:", ctime()) ## 此句执行完成,再转入主进程


def producer(sequence, output_q):
    print ("Into procuder:", ctime())
    for item in sequence:
        output_q.put(item)
        print ("put", item, "into q")
    print ("Out of procuder:", ctime())

if __name__ == '__main__':
    q = multiprocessing.Queue()
    cons_p = multiprocessing.Process(target = consumer, args = (q,))
    cons_p.start()

    sequence = [1,2,3,4]
    producer(sequence, q)

    q.put(None)
    cons_p.join()
import multiprocessing
from time import 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")
    print ("Out of consumer:", ctime())

def producer(sequence, output_q):
    for item in sequence:
        print ("Into procuder:", ctime())
        output_q.put(item)
        print ("Out of procuder:", ctime())

if __name__ == '__main__':
    q = multiprocessing.Queue()
    cons_p1 = multiprocessing.Process (target = consumer, args = (q,))
    cons_p1.start()

    cons_p2 = multiprocessing.Process (target = consumer, args = (q,))
    cons_p2.start()

    sequence = [1,2,3,4]
    producer(sequence, q)

    q.put(None)
    q.put(None)

    cons_p1.join()
    cons_p2.join()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值