没有多处理打印输出 (Spyder) [英] No multiprocessing print outputs (Spyder)

查看:36
本文介绍了没有多处理打印输出 (Spyder)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始深入研究多处理,因为我相信我的代码可以很容易地并行化.但是,在学习教程时,我遇到了一个问题:分布在池中的函数似乎无法打印.

I have recently started to delve into multiprocessing, as I believe my code can be easily parallelized. Upon working through the tutorials, though, I encountered an issue: functions distributed in a pool do not seem to print.

罪魁祸首:

__spec__ = None # This line is required for Spyder and not part of the actual example

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

我收到的输出如下:

main line 
module name: __main__ 
parent process: 10812 
process id: 11348*

现在很明显,控制台似乎只打印了 info 函数,而不是 f 函数(使用 multiprocessing.Process)的任何输出.我在网上找到的其他示例中遇到了类似的问题:使用多处理时计算已完成并正确返回,但打印件从未显示在控制台中.

Now it is clear that the console only seems to print the info function, but not any output of the f function (which is using multiprocessing.Process). I have encountered similar issues with other examples I found online: computations are done and returned correctly when using multiprocessing, but prints never show up in the console.

有人知道为什么会出现这个问题,以及如何解决这个问题吗?

Does anybody know why, and how to address this issue?

在一个可能相关的说明中,我在 Spyder 3.2.4 中使用 Python 3.6.Spyder 似乎有一些怪癖,因为代码中的第一行已经是允许多处理工作所需的解决方法,我发现这个问题已经讨论过 此处.此处提到了一个类似的未解决问题.

On a possibly related note, I am using Python 3.6 in Spyder 3.2.4 . Spyder seems to have a few quirks, as the first line in the code already is a workaround required to allow multiprocessing to work at all, an issue I found already discussed here. A similar, unresolved issue was mentioned here.

感谢您的帮助,并祝大家新年快乐.

I would appreciate any help, and a happy new year to everyone.

推荐答案

(Spyder 维护者在此)在 Windows 上的 Spyder 的 IPython 控制台中,多处理不能很好地工作.但是,您可以在外部终端中运行您的代码以获得您想要的结果.

(Spyder maintainer here) Multiprocessing doesn't work well on Windows in Spyder's IPython console. However, you can run your code in an external terminal to have the results you want.

要做到这一点,请转到

<代码>运行 >每个文件的配置 >在外部系统终端中执行

这篇关于没有多处理打印输出 (Spyder)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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