简体   繁体   English

Windows 上 Python 中的多处理

[英]Multiprocessing in Python on Windows

I am trying out the examples listed in the python docs http://docs.python.org/library/multiprocessing.html particularly 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.它虽然适用于Linux。 What is going on?到底是怎么回事?

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.如果您想要父进程的 ID,示例 2 将不起作用。 There is no getppid in windows. windows中没有getppid。

Just take the print os.getppid and execute, its brilliant as ever !只需打印 os.getppid 并执行,它一如既往的精彩!

Please refer this for more by Doug .请参阅Doug了解更多信息。 ( UPDATE : The original link isn't working, here is something similar.) 更新:原始链接不起作用,这里有类似的东西。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM