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

  1. <tfoot id='AmxfM'></tfoot>

    <legend id='AmxfM'><style id='AmxfM'><dir id='AmxfM'><q id='AmxfM'></q></dir></style></legend>
  2. <small id='AmxfM'></small><noframes id='AmxfM'>

      如何避免这种酸洗错误,在 Python 中并行化此代码

      时间:2023-05-25

      <small id='2oVpW'></small><noframes id='2oVpW'>

      • <tfoot id='2oVpW'></tfoot>
          <bdo id='2oVpW'></bdo><ul id='2oVpW'></ul>

            <tbody id='2oVpW'></tbody>

            <legend id='2oVpW'><style id='2oVpW'><dir id='2oVpW'><q id='2oVpW'></q></dir></style></legend>

              1. <i id='2oVpW'><tr id='2oVpW'><dt id='2oVpW'><q id='2oVpW'><span id='2oVpW'><b id='2oVpW'><form id='2oVpW'><ins id='2oVpW'></ins><ul id='2oVpW'></ul><sub id='2oVpW'></sub></form><legend id='2oVpW'></legend><bdo id='2oVpW'><pre id='2oVpW'><center id='2oVpW'></center></pre></bdo></b><th id='2oVpW'></th></span></q></dt></tr></i><div id='2oVpW'><tfoot id='2oVpW'></tfoot><dl id='2oVpW'><fieldset id='2oVpW'></fieldset></dl></div>
                本文介绍了如何避免这种酸洗错误,在 Python 中并行化此代码的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我有以下代码.

                def main():
                  (minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
                  for i in range(minI, maxI, iStep):
                    for j in range(minJ, maxJ, jStep): 
                      p = multiprocessing.Process(target=functionA, args=(minI, minJ))
                      p.start()
                      def functionB((a, b)):
                        subprocess.call('program1 %s %s %s %s %s %s' %(c, a, b, 'file1', 
                          'file2', 'file3'), shell=True)
                        for d in ['a', 'b', 'c']:
                          subprocess.call('program2 %s %s %s %s %s' %(d, 'file4', 'file5', 
                            'file6', 'file7'), shell=True)
                      abProduct = list(itertools.product(range(0, 10), range(0, 10)))
                      pool = multiprocessing.Pool(processes=numProcessors)
                      pool.map(functionB, abProduct) 
                

                它会产生以下错误.

                Exception in thread Thread-1:
                Traceback (most recent call last):
                  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
                    self.run()
                  File "/usr/lib64/python2.6/threading.py", line 484, in run 
                    self.__target(*self.__args, **self.__kwargs)
                  File "/usr/lib64/python2.6/multiprocessing/pool.py", line 255, in _handle_tasks
                    put(task)
                PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function fa
                iled
                

                functionA 的内容不重要,不会产生错误.当我尝试映射函数 B 时,似乎发生了错误.如何消除此错误,在 Python 2.6 中并行化此代码的最佳方法是什么?

                The contents of functionA are unimportant, and do not produce an error. The error seems to occur when I try to map functionB. How do I remove this error, and what is the best way to parallelize this code in Python 2.6?

                推荐答案

                您最有可能看到此行为的原因是您定义池、对象和函数的顺序.multiprocessing 与使用线程并不完全相同.每个进程都会生成并加载环境的副本.如果您在进程可能无法使用的范围内创建函数,或者在池之前创建对象,那么池将失败.

                The reason you are most likely seeing this behavior is because of the order in which you define your pool, objects, and functions. multiprocessing is not quite the same as using threads. Each process will spawn and load a copy of the environment. If you create functions in scopes that may not be available to the processes, or create objects before the pool, then the pool will fail.

                首先,尝试在大循环之前创建一个池:

                First, try creating one pool before your big loop:

                (minI, maxI, iStep, minJ, maxJ, jStep, a, b, numProcessors) = sys.argv
                pool = multiprocessing.Pool(processes=numProcessors)
                for i in range(minI, maxI, iStep):
                    ...
                

                然后,将您的目标可调用对象移到动态循环之外:

                Then, move your target callable outside the dynamic loop:

                def functionB(a, b):
                    ...
                
                def main():
                    ...
                

                考虑这个例子...

                坏了

                import multiprocessing
                
                def broken():
                    vals = [1,2,3]
                
                    def test(x):
                        return x
                
                    pool = multiprocessing.Pool()
                    output = pool.map(test, vals)
                    print output
                
                broken()
                # PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
                

                工作

                import multiprocessing
                
                def test(x):
                    return x
                
                def working():
                    vals = [1,2,3]
                
                    pool = multiprocessing.Pool()
                    output = pool.map(test, vals)
                    print output
                
                working()
                # [1, 2, 3]
                

                这篇关于如何避免这种酸洗错误,在 Python 中并行化此代码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:嵌入式python:多处理不起作用 下一篇:为什么 Python 多处理守护进程不打印到标准输出

                相关文章

                最新文章

                  <small id='57wZQ'></small><noframes id='57wZQ'>

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