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

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

        <bdo id='G3elr'></bdo><ul id='G3elr'></ul>

      1. <tfoot id='G3elr'></tfoot>
      2. 多处理:在某些情况下,在赋值之前引用变量,但

        时间:2023-05-26

          • <legend id='KMIhb'><style id='KMIhb'><dir id='KMIhb'><q id='KMIhb'></q></dir></style></legend>

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

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

                  本文介绍了多处理:在某些情况下,在赋值之前引用变量,但在其他情况下不引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在这个网站的某个地方找到了以下示例:

                  I found the following example on this website somewhere:

                  import multiprocessing
                  import ctypes
                  import numpy as np
                  
                  shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
                  shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
                  shared_array = shared_array.reshape(10, 10)
                  
                  # No copy was made
                  assert shared_array.base.base is shared_array_base.get_obj()
                  
                  # Parallel processing
                  def my_func(i, def_param=shared_array):
                      shared_array[i,:] = i
                  
                  if __name__ == '__main__':
                      pool = multiprocessing.Pool(processes=4)
                      pool.map(my_func, range(10))
                  
                      print shared_array
                  

                  上面的代码工作正常,但是如果我想向共享数组添加一个数组,比如 shared_array += some_other_array (而不是上面的 shared_array[i,;] = i)我得到了

                  The above code works fine, but if I want to add an array to the shared array, something like shared_array += some_other_array (instead of the above shared_array[i,;] = i) I get

                  赋值前引用的局部变量shared_array"

                  local variable 'shared_array' referenced before assignment

                  任何想法为什么我不能这样做?

                  Any ideas why I cannot do that?

                  推荐答案

                  如果一个变量被赋值给函数中的任何地方,它就会被视为一个局部变量.shared_array += some_other_array 等价于 shared_array = shared_array + some_other_array.因此 shared_array 被视为局部变量,当您尝试在赋值右侧使用它时,该变量并不存在.

                  If a variable is assigned to anywhere in a function, it is treated as a local variable. shared_array += some_other_array is equivalent to shared_array = shared_array + some_other_array. Thus shared_array is treated as a local variable, which does not exist at the time you try to use it on the right-hand side of the assignment.

                  如果你想使用全局 shared_array 变量,你需要通过在你的函数中放置一个 global shared_array 来显式地将它标记为全局变量.

                  If you want to use the global shared_array variable, you need to explicitly mark it as global by putting a global shared_array in your function.

                  您没有看到 shared_array[i,:] = i 错误的原因是它没有分配给变量 shared_array.相反,它改变了该对象,分配给它的一部分.在 Python 中,分配给一个裸名(例如,shared_array = ...)与任何其他类型的分配(例如,shared_array[...] = ...),尽管它们看起来很相似.

                  The reason you don't see the error with shared_array[i,:] = i is that this does not assign to the variable shared_array. Rather, it mutates that object, assigning to a slice of it. In Python, assigning to a bare name (e.g., shared_array = ...) is very different from any other kind of assignment (e.g., shared_array[...] = ...), even though they look similar.

                  请注意,顺便说一下,该错误与多处理无关.

                  Note, incidentally, that the error has nothing to do with multiprocessing.

                  这篇关于多处理:在某些情况下,在赋值之前引用变量,但在其他情况下不引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:具有单个函数的 Python 多处理 下一篇:共享数组在 python 多处理中未正确共享

                  相关文章

                  最新文章

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

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