Python 3 Jupyter上的多处理 [英] Multiprocessing on Python 3 Jupyter

查看:97
本文介绍了Python 3 Jupyter上的多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来这里是因为我的木星的Python3笔记本出现问题. 我需要创建一个使用多处理库的函数. 在实施它之前,我要进行一些测试. 我发现了很多不同的例子,但是问题总是一样的:我的代码被执行了,但笔记本界面上什么也没发生:

I come here because I have an issue with my Jupiter's Python3 notebook. I need to create a function that uses the multiprocessing library. Before to implement it, I make some tests. I found a looooot of different examples but the issue is everytime the same : my code is executed but nothing happens in the notebook's interface :

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

The code i try to run on jupyter is this one :

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"运行代码时就可以了,并且可以看到日志:

It's OK when I just run my code without Jupyter but with the command "python my_progrem.py" and I can see the logs :

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

Is there, for my example, and in Jupyter, a way to catch the results of my two tasks (proc1 and proc2 which both call thefunction "doubler") in a variable/object that I could use after ? If "yes", how can I do it?

推荐答案

在Jupyter笔记本中运行多处理作业的另一种方法是使用

Another way of running multiprocessing jobs in a Jupyter notebook is to use one of the approaches supported by the nbmultitask package.

这篇关于Python 3 Jupyter上的多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆