可以从 IDLE 运行多处理进程类吗

Can multiprocessing Process class be run from IDLE(可以从 IDLE 运行多处理进程类吗)
本文介绍了可以从 IDLE 运行多处理进程类吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

多处理进程类的基本示例在从文件执行时运行,而不是从 IDLE 执行.为什么会这样,能做到吗?

A basic example of multiprocessing Process class runs when executed from file, but not from IDLE. Why is that and can it be done?

from multiprocessing import Process

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

p = Process(target=f, args=('bob',))
p.start()
p.join()

推荐答案

是的.该函数中的以下工作 f 在单独的(第三个)进程中运行.

Yes. The following works in that function f is run in a separate (third) process.

from multiprocessing import Process

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

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

但是,要查看 print 输出,至少在 Windows 上,必须从这样的控制台启动 IDLE.

However, to see the print output, at least on Windows, one must start IDLE from a console like so.

C:UsersTerry>python -m idlelib
hello bob

(在 2.x 上使用 idlelib.idle.)原因是 IDLE 在单独的进程中运行用户代码.目前,IDLE 进程和用户代码进程之间的连接是通过套接字进行的.多处理完成的分叉不会复制或继承套接字连接.通过图标或资源管理器(在 Windows 中)启动 IDLE 时,打印输出无处可去.当使用 python(而不是 pythonw)从控制台启动时,输出将进入控制台,如上.

(Use idlelib.idle on 2.x.) The reason is that IDLE runs user code in a separate process. Currently the connection between the IDLE process and the user code process is via a socket. The fork done by multiprocessing does not duplicate or inherit the socket connection. When IDLE is started via an icon or Explorer (in Windows), there is nowhere for the print output to go. When started from a console with python (rather than pythonw), output goes to the console, as above.

这篇关于可以从 IDLE 运行多处理进程类吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Python GTK Drag and Drop - Get URL(Python GTK 拖放 - 获取 URL)
Drag n Drop inside QgraphicsView doesn#39;t work (PyQt)(在 QgraphicsView 内拖放不起作用(PyQt))
How to get dropped file names in PyQt/PySide(如何在 PyQt/PySide 中获取删除的文件名)
Drag and drop onto Python script in Windows Explorer(在 Windows 资源管理器中拖放到 Python 脚本)
In Python, how can I include (not import) one file within another file, macro style, without changing namespace?(在 Python 中,如何在不更改命名空间的情况下在另一个文件中包含(不导入)一个文件,宏样式?)
C Preprocessor Macro equivalent for Python(相当于 Python 的 C 预处理器宏)