【问题标题】:Python multiprocessing for dummies [closed]假人的Python多处理[关闭]
【发布时间】:2015-09-15 19:27:25
【问题描述】:

我正在尝试找到一个简单的示例,清楚地显示单个任务被划分为多处理。

坦率地说,许多示例都过于复杂,从而使流程更难玩。

有人愿意分享他们的突破性样本或示例吗?

【问题讨论】:

    标签: python multiprocessing


    【解决方案1】:

    你的基本例子是这样的:

    >>> import multiprocessing as mp
    >>> from math import sqrt
    >>> worker_pool = mp.Pool()
    >>> jobs = [0, 1, 4, 9, 16, 25] 
    >>>
    >>> # calculate jobs in blocking batch parallel
    >>> results = worker_pool.map(sqrt, jobs)
    >>> results
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    >>>
    >>> # calculate jobs in asynchronous parallel
    >>> results = worker_pool.map_async(sqrt, jobs)
    >>> results.get()
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    >>>
    >>> # calculate jobs in parallel with an unordered iterator
    >>> results = worker_pool.imap_unordered(sqrt, jobs)
    >>> list(results)  # NOTE: results may return out of order
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    >>>
    >>> # a single blocking job on another process
    >>> worker_pool.apply(sqrt, [9])
    3.0
    >>> # a single asynchronous job on another process
    >>> y = worker_pool.apply_async(sqrt, [9])
    >>> y.get()
    3.0
    >>> # the same interface exists for threads
    >>> thread_pool = mp.dummy.Pool()
    >>> thread_pool.map(sqrt, jobs)
    [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    >>>
    >>> # finishing up, you should shut down your pools
    >>> worker_pool.close()
    >>> worker_pool.join()
    >>> thread_pool.close()
    >>> thread_pool.join()
    

    如果您不想批量并行,但想要更复杂的东西,示例可能会变得更复杂。

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2017-11-19
      • 2022-01-02
      • 2015-02-11
      相关资源
      最近更新 更多