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

        python multiprocessing vs threading for cpu bound work on win

        时间:2023-05-26

        <tfoot id='y0tMH'></tfoot>
      1. <legend id='y0tMH'><style id='y0tMH'><dir id='y0tMH'><q id='y0tMH'></q></dir></style></legend>
          <bdo id='y0tMH'></bdo><ul id='y0tMH'></ul>

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

              1. <i id='y0tMH'><tr id='y0tMH'><dt id='y0tMH'><q id='y0tMH'><span id='y0tMH'><b id='y0tMH'><form id='y0tMH'><ins id='y0tMH'></ins><ul id='y0tMH'></ul><sub id='y0tMH'></sub></form><legend id='y0tMH'></legend><bdo id='y0tMH'><pre id='y0tMH'><center id='y0tMH'></center></pre></bdo></b><th id='y0tMH'></th></span></q></dt></tr></i><div id='y0tMH'><tfoot id='y0tMH'></tfoot><dl id='y0tMH'><fieldset id='y0tMH'></fieldset></dl></div>
                    <tbody id='y0tMH'></tbody>
                  本文介绍了python multiprocessing vs threading for cpu bound work on windows and linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  所以我敲了一些测试代码,看看多处理模块在 cpu 绑定工作上与线程相比如何扩展.在 linux 上,我得到了预期的性能提升:

                  So I knocked up some test code to see how the multiprocessing module would scale on cpu bound work compared to threading. On linux I get the performance increase that I'd expect:

                  linux (dual quad core xeon):
                  serialrun took 1192.319 ms
                  parallelrun took 346.727 ms
                  threadedrun took 2108.172 ms
                  

                  我的双核 macbook pro 显示相同的行为:

                  My dual core macbook pro shows the same behavior:

                  osx (dual core macbook pro)
                  serialrun took 2026.995 ms
                  parallelrun took 1288.723 ms
                  threadedrun took 5314.822 ms
                  

                  然后我在一台windows机器上试了一下,得到了一些非常不同的结果.

                  I then went and tried it on a windows machine and got some very different results.

                  windows (i7 920):
                  serialrun took 1043.000 ms
                  parallelrun took 3237.000 ms
                  threadedrun took 2343.000 ms

                  为什么,为什么,Windows 上的多处理方法这么慢?

                  Why oh why, is the multiprocessing approach so much slower on windows?

                  这是测试代码:

                  #!/usr/bin/env python
                  
                  import multiprocessing
                  import threading
                  import time
                  
                  def print_timing(func):
                      def wrapper(*arg):
                          t1 = time.time()
                          res = func(*arg)
                          t2 = time.time()
                          print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
                          return res
                      return wrapper
                  
                  
                  def counter():
                      for i in xrange(1000000):
                          pass
                  
                  @print_timing
                  def serialrun(x):
                      for i in xrange(x):
                          counter()
                  
                  @print_timing
                  def parallelrun(x):
                      proclist = []
                      for i in xrange(x):
                          p = multiprocessing.Process(target=counter)
                          proclist.append(p)
                          p.start()
                  
                      for i in proclist:
                          i.join()
                  
                  @print_timing
                  def threadedrun(x):
                      threadlist = []
                      for i in xrange(x):
                          t = threading.Thread(target=counter)
                          threadlist.append(t)
                          t.start()
                  
                      for i in threadlist:
                          i.join()
                  
                  def main():
                      serialrun(50)
                      parallelrun(50)
                      threadedrun(50)
                  
                  if __name__ == '__main__':
                      main()

                  推荐答案

                  进程在 UNIX 变体下更加轻量级.Windows 进程很繁重,需要更多时间才能启动.线程是在 Windows 上进行多处理的推荐方式.

                  Processes are much more lightweight under UNIX variants. Windows processes are heavy and take much more time to start up. Threads are the recommended way of doing multiprocessing on windows.

                  这篇关于python multiprocessing vs threading for cpu bound work on windows and linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Pandas df.iterrows() 并行化 下一篇:具有分布式集群的 Python 多处理

                  相关文章

                  最新文章

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

                      <bdo id='BXb1N'></bdo><ul id='BXb1N'></ul>
                      <tfoot id='BXb1N'></tfoot>