Windows机器上的IPython控制台中的多处理 - 如果__name_要求 [英] multiprocessing in IPython console on Windows machine - if __name_ requirement

查看:93
本文介绍了Windows机器上的IPython控制台中的多处理 - 如果__name_要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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上这需要如果__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

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

  1. Put the defintion of rand_string in a separate file, called test2.
  2. Import test2 as module into my test.py script

将test2导入为test2

修改以下行以访问 test2 模块

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


  • 运行 test.py

    致电 myFunction ()

    快乐:)

    该解决方案基于此多处理教程表示从另一个脚本导入目标函数。此解决方案绕过的安全自导入,如果__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.

    这篇关于Windows机器上的IPython控制台中的多处理 - 如果__name_要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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