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

      1. <small id='YtnJ5'></small><noframes id='YtnJ5'>

      2. 具有单个函数的 Python 多处理

        时间:2023-05-26
          <tbody id='Rso1o'></tbody>
        <legend id='Rso1o'><style id='Rso1o'><dir id='Rso1o'><q id='Rso1o'></q></dir></style></legend>

      3. <small id='Rso1o'></small><noframes id='Rso1o'>

            • <tfoot id='Rso1o'></tfoot>

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

                  本文介绍了具有单个函数的 Python 多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个当前正在运行的模拟,但 ETA 大约需要 40 小时 - 我正在尝试通过多处理来加速它.

                  I have a simulation that is currently running, but the ETA is about 40 hours -- I'm trying to speed it up with multi-processing.

                  它本质上迭代了一个变量 (L) 的 3 个值,以及第二个变量 (a) 的 99 个值.使用这些值,它实际上运行了一个复杂的模拟并返回 9 个不同的标准偏差.因此(尽管我还没有这样编码)它本质上是一个函数,它接受两个值作为输入 (L,a) 并返回 9 个值.

                  It essentially iterates over 3 values of one variable (L), and over 99 values of of a second variable (a). Using these values, it essentially runs a complex simulation and returns 9 different standard deviations. Thus (even though I haven't coded it that way yet) it is essentially a function that takes two values as inputs (L,a) and returns 9 values.

                  这是我拥有的代码的精髓:

                  Here is the essence of the code I have:

                  STD_1 = []
                  STD_2 = []
                  # etc.
                  
                  for L in range(0,6,2):
                      for a in range(1,100):
                          ### simulation code ###
                          STD_1.append(value_1)
                          STD_2.append(value_2)
                          # etc.
                  

                  以下是我可以修改的内容:

                  Here is what I can modify it to:

                  master_list = []
                  
                  def simulate(a,L):
                      ### simulation code ###
                      return (a,L,STD_1, STD_2 etc.)
                  
                  for L in range(0,6,2):
                      for a in range(1,100): 
                          master_list.append(simulate(a,L))
                  

                  由于每个模拟都是独立的,因此它似乎是实现某种多线程/处理的理想场所.

                  Since each of the simulations are independent, it seems like an ideal place to implement some sort of multi-threading/processing.

                  我将如何编写这个代码?

                  How exactly would I go about coding this?

                  另外,是否所有内容都会按顺序返回到主列表,或者如果多个进程正在工作,它可能会出现故障?

                  Also, will everything be returned to the master list in order, or could it possibly be out of order if multiple processes are working?

                  编辑 2:这是我的代码——但它运行不正确.它询问我是否想在我运行程序后立即终止它.

                  EDIT 2: This is my code -- but it doesn't run correctly. It asks if I want to kill the program right after I run it.

                  import multiprocessing
                  
                  data = []
                  
                  for L in range(0,6,2):
                      for a in range(1,100):
                          data.append((L,a))
                  
                  print (data)
                  
                  def simulation(arg):
                      # unpack the tuple
                      a = arg[1]
                      L = arg[0]
                      STD_1 = a**2
                      STD_2 = a**3
                      STD_3 = a**4
                      # simulation code #
                      return((STD_1,STD_2,STD_3))
                  
                  print("1")
                  
                  p = multiprocessing.Pool()
                  
                  print ("2")
                  
                  results = p.map(simulation, data)
                  

                  编辑 3:还有什么是多处理的限制.我听说它不能在 OS X 上运行.这是正确的吗?

                  EDIT 3: Also what are the limitations of multiprocessing. I've heard that it doesn't work on OS X. Is this correct?

                  推荐答案

                  • 将每次迭代的数据包装成一个元组.
                  • 列出这些元组的data
                  • 编写函数f处理一个元组并返回一个结果
                  • 创建 p = multiprocessing.Pool() 对象.
                  • 调用results = p.map(f, data)
                    • Wrap the data for each iteration up into a tuple.
                    • Make a list data of those tuples
                    • Write a function f to process one tuple and return one result
                    • Create p = multiprocessing.Pool() object.
                    • Call results = p.map(f, data)
                    • 这将运行尽可能多的 f 实例,因为您的机器在不同进程中拥有内核.

                      This will run as many instances of f as your machine has cores in separate processes.

                      Edit1:示例:

                      from multiprocessing import Pool
                      
                      data = [('bla', 1, 3, 7), ('spam', 12, 4, 8), ('eggs', 17, 1, 3)]
                      
                      def f(t):
                          name, a, b, c = t
                          return (name, a + b + c)
                      
                      p = Pool()
                      results = p.map(f, data)
                      print results
                      

                      多处理应该可以在 OSX 等类 UNIX 平台上正常工作.只有缺少 os.fork 的平台(主要是 MS Windows)需要特别注意.但即使在那里它仍然有效.请参阅多处理文档.

                      Multiprocessing should work fine on UNIX-like platforms such as OSX. Only platforms that lack os.fork (mainly MS Windows) need special attention. But even there it still works. See the multiprocessing documentation.

                      这篇关于具有单个函数的 Python 多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:多处理中的error_callback.Python 2中的池apply_async? 下一篇:多处理:在某些情况下,在赋值之前引用变量,但

                  相关文章

                  最新文章

                  <legend id='5WY1m'><style id='5WY1m'><dir id='5WY1m'><q id='5WY1m'></q></dir></style></legend>

                • <tfoot id='5WY1m'></tfoot>

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

                      <small id='5WY1m'></small><noframes id='5WY1m'>

                      • <bdo id='5WY1m'></bdo><ul id='5WY1m'></ul>