• 多进程间处理--信号处理


    信号处理:

    signal.signal(signum,handler)

    功能:处理一个信号

    参数:signum:要处理的信号,

       handler:该信号的处理方法

            SIG_DFL   采用默认方法

            SIG_IGN   忽略这个信号

            func          自定义的方法处理

    func格式要求

    def  fuc(sig,frame)

      ......

    sig:接收到的信号

    frame:信号对象

    ======================================================

    *singal函数是一个异步处理信号函数,只要执行,在进程中就会按照指定方法处理信号;

    *signal不能处理SIGSTOP SIGKILL信号

    ======================================================

    import singal
    from time import sleep
    singal.alarm(5)
    
    #采用默认的方法来处理
    singal.singal(singal.SIGALRM,singal.SIG_DFL)
    
    #忽略信号
    singal.singal(singal.SIGALRM,singal.SIG_IGN)
    
    
    
    while  True:
        sleep(2)
        print('wating........... ')
    from signal import *
    import time
    #有固定格式要求
    def handler(sig,frame1):
        if sig==SIGALRM:
            print("收到时钟信号")
        elif sig==SIGINT:
            print("无法退出")    
    
    alarm(7)
    
    #通过自定义方法处理,SIGALRM表示时钟函数,SIGINT表示ctrl
    #+c
    signal(SIGALRM,handler)
    signal(SIGINT,handler)
    
    
    while True:
        print('waiting for a singal')
        time.sleep(2)

    信号量

    --给定一定的信号数量,对多个进程可见,并且多个进程均可以操作,进程根据信号量的多少,可以有不同的行为

    multiprocess--------->Semaphore()

    sem=Semaphore(num)

    功能:定义信号量

    参数:num:给定信号量的初始个数

    返回值:返回信号量对象

    sem.acquire()  将信号量 减一   信号量为0时,调用会阻塞

    sem.release()  将信号量 加一

    ===========================

    multiprocessing.current_process():获取当前进程对象

    ===================================

    from multiprocessing import  
    Semaphore ,Process,current_process
    
    from time import sleep
    #创建信号量初始值为3
    #创建了4个进程后,去执行fun函数,第一句执行的sem.qcquire()使初始量减1,当3个进程执行后,此时
    值为0,这时候没有执行的进程形成阻塞。等到有进程执行sem.release后,信号量加1,阻塞结束,开始执行
    下一语句
    #多个进程共同拥有信号量
    sem=Semaphore(3) def fun(): print("进程%s等待信号量"%current_process()) sem.acquire() print("进程%s消耗信号量"%current_process()) sleep(2) print("进程%s添加信号量"%current_process()) sem.release() jobs=[] for i in range(4): p=Process(target=fun) p.start() jobs.append(p) for i in jobs: i.join()
    yangrui@ubuntu:~/num6$ python3 semphore.py 
    进程<Process(Process-1, started)>等待信号量
    进程<Process(Process-1, started)>消耗信号量
    进程<Process(Process-2, started)>等待信号量
    进程<Process(Process-2, started)>消耗信号量
    进程<Process(Process-3, started)>等待信号量
    进程<Process(Process-3, started)>消耗信号量
    进程<Process(Process-4, started)>等待信号量
    进程<Process(Process-1, started)>添加信号量
    进程<Process(Process-4, started)>消耗信号量
    进程<Process(Process-3, started)>添加信号量
    进程<Process(Process-2, started)>添加信号量
    进程<Process(Process-4, started)>添加信号量
    结果

     =========================================

    使用处理僵尸进程

    在父进程中,忽略子进程的发送信号

    signal(SIGCHLD,SIG_IGN)

    ==========================================

  • 相关阅读:
    AcWing 204. 表达整数的奇怪方式 / Strange Way To Express Integers
    Codeforces Edu Round 67 A-C + E
    Codeforces Edu Round 66 A-E
    Codeforces Edu Round 65 A-E
    Codeforces Edu Round 64 A-D
    Codeforces Edu Round 63 A-E
    Codeforces Edu Round 62 A-E
    Codeforces Edu Round 61 A-C + F
    python 线程池和锁
    python 线程
  • 原文地址:https://www.cnblogs.com/sike8/p/11149433.html
Copyright © 2020-2023  润新知