<legend id='3kDOW'><style id='3kDOW'><dir id='3kDOW'><q id='3kDOW'></q></dir></style></legend>
  • <small id='3kDOW'></small><noframes id='3kDOW'>

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

      1. 具有全局变量的 multiprocessing.Pool

        时间:2023-05-26

          • <bdo id='dm7wP'></bdo><ul id='dm7wP'></ul>
            <legend id='dm7wP'><style id='dm7wP'><dir id='dm7wP'><q id='dm7wP'></q></dir></style></legend>
              <tfoot id='dm7wP'></tfoot>

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

                  本文介绍了具有全局变量的 multiprocessing.Pool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 python 多处理库中的 Pool 类编写一个将在 HPC 集群上运行的程序.

                  I am using the Pool class from python's multiprocessing library write a program that will run on an HPC cluster.

                  这是我正在尝试做的抽象:

                  Here is an abstraction of what I am trying to do:

                  def myFunction(x):
                      # myObject is a global variable in this case
                      return myFunction2(x, myObject)
                  
                  def myFunction2(x,myObject):
                      myObject.modify() # here I am calling some method that changes myObject
                      return myObject.f(x)
                  
                  poolVar = Pool()
                  argsArray = [ARGS ARRAY GOES HERE]
                  output = poolVar.map(myFunction, argsArray)
                  

                  函数 f(x) 包含在 *.so 文件中,即它正在调用 C 函数.

                  The function f(x) is contained in a *.so file, i.e., it is calling a C function.

                  我遇到的问题是每次运行程序时输出变量的值都不同(即使函数 myObject.f() 是确定性函数).(如果我只有一个进程,那么每次运行程序时输出变量都是相同的.)

                  The problem I am having is that the value of the output variable is different each time I run my program (even though the function myObject.f() is a deterministic function). (If I only have one process then the output variable is the same each time I run the program.)

                  我尝试创建对象而不是将其存储为全局变量:

                  I have tried creating the object rather than storing it as a global variable:

                  def myFunction(x):
                      myObject = createObject()
                      return myFunction2(x, myObject)
                  

                  然而,在我的程序中,对象的创建成本很高,因此,创建一次 myObject 然后在每次调用 myFunction2() 时修改它要容易得多.因此,我不想每次都创建对象.

                  However, in my program the object creation is expensive, and thus, it is a lot easier to create myObject once and then modify it each time I call myFunction2(). Thus, I would like to not have to create the object each time.

                  你有什么建议吗?我对并行编程很陌生,所以我可能会做错这一切.我决定使用 Pool 类,因为我想从简单的东西开始.但我愿意尝试更好的方法.

                  Do you have any tips? I am very new to parallel programming so I could be going about this all wrong. I decided to use the Pool class since I wanted to start with something simple. But I am willing to try a better way of doing it.

                  推荐答案

                  我正在使用 python 多处理库中的 Pool 类来做HPC 集群上的一些共享内存处理.

                  进程不是线程!不能简单地将 Thread 替换为 Process 并期望所有进程都能正常工作.进程共享内存,这意味着全局变量被复制,因此它们在原始进程中的值不会改变.

                  Processes are not threads! You cannot simply replace Thread with Process and expect all to work the same. Processes do not share memory, which means that the global variables are copied, hence their value in the original process doesn't change.

                  如果你想在进程之间使用共享内存那么你必须使用multiprocessing的数据类型,例如ValueArray、或使用 Manager 创建共享列表等.

                  If you want to use shared memory between processes then you must use the multiprocessing's data types, such as Value, Array, or use the Manager to create shared lists etc.

                  您可能对 Manager.register 方法感兴趣,该方法允许 Manager 创建共享的自定义对象(尽管它们必须是可挑选的).

                  In particular you might be interested in the Manager.register method, which allows the Manager to create shared custom objects(although they must be picklable).

                  但是我不确定这是否会提高性能.由于进程之间的任何通信都需要酸洗,而酸洗通常需要更多时间,然后只是实例化对象.

                  However I'm not sure whether this will improve the performance. Since any communication between processes requires pickling, and pickling takes usually more time then simply instantiating the object.

                  请注意,您可以在创建 initializer 和 initargs 参数对工作进程进行一些初始化.org/3.3/library/multiprocessing.html#multiprocessing.pool.Pool" rel="noreferrer">Pool.

                  Note that you can do some initialization of the worker processes passing the initializer and initargs argument when creating the Pool.

                  例如,以最简单的形式,在工作进程中创建一个全局变量:

                  For example, in its simplest form, to create a global variable in the worker process:

                  def initializer():
                      global data
                      data = createObject()
                  

                  用作:

                  pool = Pool(4, initializer, ())
                  

                  那么worker函数就可以放心的使用data全局变量了.

                  Then the worker functions can use the data global variable without worries.

                  样式说明:从不为您的变量/模块使用内置名称.在您的情况下, object 是内置的.否则,您最终会遇到意想不到的错误,这些错误可能晦涩难懂且难以追踪.

                  Style note: Never use the name of a built-in for your variables/modules. In your case object is a built-in. Otherwise you'll end up with unexpected errors which may be obscure and hard to track down.

                  这篇关于具有全局变量的 multiprocessing.Pool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 multiprocessing.Manager.list 而不是真正的列表会 下一篇:Windows 机器上 IPython 控制台中的多处理 - 如果 _

                  相关文章

                  最新文章

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

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

                      <bdo id='BXF55'></bdo><ul id='BXF55'></ul>
                  1. <tfoot id='BXF55'></tfoot>

                    1. <legend id='BXF55'><style id='BXF55'><dir id='BXF55'><q id='BXF55'></q></dir></style></legend>