Windows 机器上 IPython 控制台中的多处理 - 如果 __name_ 要求

2022-01-12 00:00:00 python windows multiprocessing ipython

问题描述

我在 Windows 机器上使用 IPython 和 Spyder IDE.当 IDE 启动时,会加载一组 py 文件来定义一些使我的工作更轻松的函数.一切正常.

I'm working with IPython and Spyder IDE on a Windows machine. When the IDE is starting, a set of py-files is loaded to define some functions that make my work a bit easier. Everything works as expected.

现在我想升级其中一个功能以使用多处理,但在 Windows 上,这需要 if __name__ == "__main__": 语句.因此,我似乎无法直接调用该函数并从 IPython 控制台传递参数.

Now I would like to upgrade one of these function to use multiprocessing, but on Windows this requires the if __name__ == "__main__": statement. So it seems that I cannot call the function directly and pass the arguments from the IPython console.

例如,其中一个 py 文件(我们称之为 test.py)可能类似于以下代码.

For example one of the py-files (let's call it test.py) could look like the following code.

import multiprocessing as mp
import random
import string

# define a example function
def rand_string(length, output):
    """ Generates a random string of numbers, lower- and uppercase chars. """
    rand_str = ''.join(random.choice(
                string.ascii_lowercase
                + string.ascii_uppercase
                + string.digits)
           for i in range(length))
    output.put(rand_str)


def myFunction():
    # Define an output queue
    output = mp.Queue()        

    # Setup a list of processes that we want to run
    processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]

    # Run processes
    for p in processes:
        p.start()

    # Exit the completed processes
    for p in processes:
        p.join()

    # Get process results from the output queue
    results = [output.get() for p in processes]

    print(results)

在我的 IPython 控制台中,我想使用该行

In my IPython console I would like to use the line

myFunction()

触发所有计算.但在 Windows 上,最终会出现 BrokenPipe 错误.

to trigger all the calculations. But on Windows a end up getting a BrokenPipe error.

当我放

if __name__ == "__main__":
     myFunction()

在 py 文件的末尾并运行完整的文件

at the end of the py-file and run the complete file by

runfile(test.py)

它有效.当然.但这使得向函数传递参数变得非常困难,因为我总是必须编辑 test.py 文件本身.

it works. Of course. But that makes it very hard to pass arguments to the function as I always have to edit the test.py-file itself.


解决方案

所以,我解决了那个具体问题.

So, I solved that specific problem.

  1. rand_string的定义放在一个单独的文件中,叫做test2.

  1. Put the defintion of rand_string in a separate file, called test2.

test2 作为模块导入我的 test.py 脚本

Import test2 as module into my test.py script

将 test2 导入为 test2

修改以下行以访问 test2 模块

modify the following line to access the test2 module

processes = [mp.Process(target=test2.rand_string, args=(5, output)) for x in range(4)]

  • 运行 test.py

    调用myFunction()

    要快乐:)

    解决方案基于此多处理教程建议从另一个脚本导入目标函数.此解决方案绕过 if __name__ -wrapper 的安全自导入以访问目标函数.

    The solution is based on this multiprocessing tutorial that suggests to import the target function from another script. This solution bypasses the safe self import by the if __name__ -wrapper to get access to the target function.

  • 相关文章