推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
oldbird
V2EX  ›  Python

多进程中 map 和 apply_async 是什么区别?

  •  
  •   oldbird · Jun 13, 2020 · 3471 views
    This topic created in 2295 days ago, the information mentioned may be changed or developed.

    pool.map()

    与

    pool.apply_async()

    pool.stop()

    pool.join()

    什么区别?

    3 replies  •  2020-06-14 19:31:24 +08:00
    lithbitren
        1
    lithbitren  
       Jun 13, 2020
    map 放在进程池里并行计算,这个语句是阻塞的,apply_async 单个函数放进池里计算非阻塞的。
    stop 没见过,join 是阻塞到所有 apply_async 产生的进程执行结束
    oldbird
        2
    oldbird  
    OP
       Jun 14, 2020
    @lithbitren 谢谢,记错了,不是 stop,是 close
    在使用 map 时,发现有的直接写到 pool.map()就可以了,有的还在后面加上 pool.close()、pool.join(),不知道这两种用法什么区别?
    lithbitren
        3
    lithbitren  
       Jun 14, 2020
    @oldbird 官方文档其实讲的都听清楚的,不写 close 一般是 map 放在 with 的块里面,块结束的时候会自动关闭,一般不用添加其他语句,apply_async 一般都不放块里,所以要手动关闭,关闭了以后才能 join
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2507 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    ♥ Do have faith in what you're doing.