Python 3 Jupyter 上的多处理

标签 multiprocessing jupyter-notebook python-3.6

我来这里是因为我的 Jupiter 的 Python3 笔记本有问题。
我需要创建一个使用多处理库的函数。
在实现它之前,我做了一些测试。
我发现了很多不同的例子,但问题每次都是一样的:我的代码已执行,但笔记本界面中没有任何 react :

enter image description here

我尝试在 jupyter 上运行的代码是这样的:

import os

from multiprocessing import Process, current_process


def doubler(number):
    """
    A doubling function that can be used by a process
    """
    result = number * 2
    proc_name = current_process().name
    print('{0} doubled to {1} by: {2}'.format(
        number, result, proc_name))
    return result


if __name__ == '__main__':
    numbers = [5, 10, 15, 20, 25]
    procs = []
    proc = Process(target=doubler, args=(5,))

    for index, number in enumerate(numbers):
        proc = Process(target=doubler, args=(number,))
        proc2 = Process(target=doubler, args=(number,))
        procs.append(proc)
        procs.append(proc2)
        proc.start()
        proc2.start()

    proc = Process(target=doubler, name='Test', args=(2,))
    proc.start()
    procs.append(proc)

    for proc in procs:
        proc.join()

当我在没有 Jupyter 的情况下运行我的代码但使用命令“python my_progrem.py”时,我可以看到日志:
enter image description here

对于我的示例,在 Jupyter 中,是否有一种方法可以在我可以在之后使用的变量/对象中捕获我的两个任务(proc1 和 proc2,它们都调用函数“doubler”)的结果?
如果"is",我该怎么做?

最佳答案

在 Jupyter notebook 中运行多处理作业的另一种方法是使用 nbmultitask 支持的方法之一。包。

关于Python 3 Jupyter 上的多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50937362/

相关文章:

Python 多线程 - 使用 While 语句运行时未释放内存

python - Python3.x RuntimeError:事件循环已关闭

python - python中的多处理以加速函数

python - 如何使用 multiprocessing.Pool 处理无法 pickle 的函数

python - 为什么我没有看到通过 Python 中的多处理加速?

django - 在 Centos7 上将 SQLite3 与 Django 2.2 和 Python 3.6.7 结合使用

numpy - 轴0超出维度0数组的范围

python - 多处理池 'apply_async' 似乎只调用一次函数

python - Jupyter 内核死于 nbconvert 但不是在 jupyter

python - 为什么 %time 输出是 Wall time : 0 ns in Jupyter Notebook with IPython?