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

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

问题描述

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

这是罪魁祸首:

__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).我在网上发现的其他示例也遇到了类似的问题:使用多重处理时,计算已完成并正确返回,但是打印内容永远不会显示在控制台中.

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

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

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

解决方案

(此处为 Spyder维护者)在Windows的Spyder IPython控制台中,多处理效果不佳.但是,您可以在外部终端中运行代码以获得所需的结果.

为此,请转到

Run > Configuration per file > Execute in an external system terminal

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.

Here's the culprit:

__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()

The output I receive is the following:

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

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?

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 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.

To do that, please go to

Run > Configuration per file > Execute in an external system terminal

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

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