美文网首页
python使用ProcessPoolExecutor问题总结

python使用ProcessPoolExecutor问题总结

作者: 托贝多尔 | 来源:发表于2022-03-21 18:00 被阅读0次

    python版本:3.7.10

    • 进程池启动需要从主进入__name__=="__mian__'
    • 进程池的future任务函数不能用装饰器

    使用示例:

    
    
    def consume(a=1):
        def wrapper(func):
            def _inner(*args, **kwargs):
                import time
                s = time.time()
                func(*args, **kwargs)
                e = time.time()
                print(f'{a}耗时:', e - s)
    
            return _inner
    
        return wrapper
    
    
    @consume
    def te(times):
        time.sleep(times)
        import os #获取pid必须在局部导入os
        pid = os.getpid()
        print(f'pid:{pid}被装饰函数{times}')
    def tes(b):
        te(b)
    
    if __name__ == '__main__':#进程池要求这样做
        from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
        from concurrent.futures import ProcessPoolExecutor, Executor
        import multiprocessing
    
        cup_num = multiprocessing.cpu_count()
        print(cup_num)
        # exec = ThreadPoolExecutor(max_workers=2, thread_name_prefix='pythonThreadPool')
        # exec = ProcessPoolExecutor(max_workers=cup_num)
        # with ThreadPoolExecutor(max_workers=cup_num) as exec:
        with ProcessPoolExecutor(max_workers=cup_num) as exec:
            exec.map(te, range(10))
            # tasks = [exec.submit(te, i) for i in range(10)]
        # wait(tasks, return_when=ALL_COMPLETED)
        # exec.shutdown()
    
    

    相关文章

      网友评论

          本文标题:python使用ProcessPoolExecutor问题总结

          本文链接:https://www.haomeiwen.com/subject/xhytjrtx.html