• <bdo id='Jt8yG'></bdo><ul id='Jt8yG'></ul>

      <legend id='Jt8yG'><style id='Jt8yG'><dir id='Jt8yG'><q id='Jt8yG'></q></dir></style></legend>

    1. <i id='Jt8yG'><tr id='Jt8yG'><dt id='Jt8yG'><q id='Jt8yG'><span id='Jt8yG'><b id='Jt8yG'><form id='Jt8yG'><ins id='Jt8yG'></ins><ul id='Jt8yG'></ul><sub id='Jt8yG'></sub></form><legend id='Jt8yG'></legend><bdo id='Jt8yG'><pre id='Jt8yG'><center id='Jt8yG'></center></pre></bdo></b><th id='Jt8yG'></th></span></q></dt></tr></i><div id='Jt8yG'><tfoot id='Jt8yG'></tfoot><dl id='Jt8yG'><fieldset id='Jt8yG'></fieldset></dl></div>
      1. <small id='Jt8yG'></small><noframes id='Jt8yG'>

        <tfoot id='Jt8yG'></tfoot>

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

        时间:2023-05-26
      2. <i id='UOgEg'><tr id='UOgEg'><dt id='UOgEg'><q id='UOgEg'><span id='UOgEg'><b id='UOgEg'><form id='UOgEg'><ins id='UOgEg'></ins><ul id='UOgEg'></ul><sub id='UOgEg'></sub></form><legend id='UOgEg'></legend><bdo id='UOgEg'><pre id='UOgEg'><center id='UOgEg'></center></pre></bdo></b><th id='UOgEg'></th></span></q></dt></tr></i><div id='UOgEg'><tfoot id='UOgEg'></tfoot><dl id='UOgEg'><fieldset id='UOgEg'></fieldset></dl></div>

            <legend id='UOgEg'><style id='UOgEg'><dir id='UOgEg'><q id='UOgEg'></q></dir></style></legend>

            <small id='UOgEg'></small><noframes id='UOgEg'>

              <tbody id='UOgEg'></tbody>
            1. <tfoot id='UOgEg'></tfoot>
                <bdo id='UOgEg'></bdo><ul id='UOgEg'></ul>

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

                  问题描述

                  限时送ChatGPT账号..

                  我在 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.

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

                • 上一篇:具有全局变量的 multiprocessing.Pool 下一篇:多处理进程中的共享状态

                  相关文章

                  最新文章

                  <small id='9sRMy'></small><noframes id='9sRMy'>

                      <legend id='9sRMy'><style id='9sRMy'><dir id='9sRMy'><q id='9sRMy'></q></dir></style></legend>

                      <i id='9sRMy'><tr id='9sRMy'><dt id='9sRMy'><q id='9sRMy'><span id='9sRMy'><b id='9sRMy'><form id='9sRMy'><ins id='9sRMy'></ins><ul id='9sRMy'></ul><sub id='9sRMy'></sub></form><legend id='9sRMy'></legend><bdo id='9sRMy'><pre id='9sRMy'><center id='9sRMy'></center></pre></bdo></b><th id='9sRMy'></th></span></q></dt></tr></i><div id='9sRMy'><tfoot id='9sRMy'></tfoot><dl id='9sRMy'><fieldset id='9sRMy'></fieldset></dl></div>
                      <tfoot id='9sRMy'></tfoot>
                        <bdo id='9sRMy'></bdo><ul id='9sRMy'></ul>