Windows 上的 Python 多处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8639745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:44:16  来源:igfitidea点击:

Multiprocessing in Python on Windows

pythonwindows

提问by Bruce

I am trying out the examples listed in the python docs http://docs.python.org/library/multiprocessing.htmlparticularly these two on Windows:

我正在尝试 Python 文档http://docs.python.org/library/multiprocessing.html 中列出的示例, 特别是 Windows 上的这两个示例:

1)

1)

from multiprocessing import Process

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

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

2)

2)

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

Here is the problem: I don't get any output from the child process. It works on Linux though. What is going on?

问题是:我没有从子进程中得到任何输出。不过它适用于Linux。到底是怎么回事?

采纳答案by King

example 1 works well.( I hope you saved the program in a file and then executed it else it will not recognise the function f at all).

示例 1 运行良好。(我希望您将程序保存在一个文件中然后执行它,否则它根本无法识别函数 f)。

example 2 won't work if u want the parent process's id. There is no getppid in windows.

如果您想要父进程的 ID,示例 2 将不起作用。Windows 中没有 getppid。

Just take the print os.getppid and execute, its brilliant as ever !

只需打印 os.getppid 并执行,它一如既往的精彩!

Please refer this for more by Doug

请参阅Doug 的更多信息