python multiprocessing pool terminate

后端 未结 4 1710
攒了一身酷
攒了一身酷 2020-12-23 23:14

I\'m working on a renderfarm, and I need my clients to be able to launch multiple instances of a renderer, without blocking so the client can receive new commands. I\'ve got

4条回答
  •  难免孤独
    2020-12-23 23:58

    # -*- coding:utf-8 -*-
    import multiprocessing
    import time
    import sys
    import threading
    from functools import partial
    
    
    #> work func
    def f(a,b,c,d,e):
        print('start')
        time.sleep(4)
        print(a,b,c,d,e)
    
    ###########> subProcess func
    #1. start a thead for work func
    #2. waiting thead with a timeout
    #3. exit the subProcess
    ###########
    def mulPro(f, *args, **kwargs):
        timeout = kwargs.get('timeout',None)
    
        #1. 
        t = threading.Thread(target=f, args=args)
        t.setDaemon(True)
        t.start()
        #2. 
        t.join(timeout)
        #3. 
        sys.exit()
    
    if __name__ == "__main__":
    
        p = multiprocessing.Pool(5)
        for i in range(5):
            #1. process the work func with "subProcess func"
            new_f = partial(mulPro, f, timeout=8)
            #2. fire on
            p.apply_async(new_f, args=(1,2,3,4,5),)
    
            # p.apply_async(f, args=(1,2,3,4,5), timeout=2)
        for i in range(10):
            time.sleep(1)
            print(i+1,"s")
    
        p.close()
        # p.join()
    

提交回复
热议问题