• <bdo id='QgFhK'></bdo><ul id='QgFhK'></ul>

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

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

    2. <small id='QgFhK'></small><noframes id='QgFhK'>

      1. 将 100% 的内核与多处理模块一起使用

        时间:2023-05-26

          <tbody id='CWOqH'></tbody>

              <tfoot id='CWOqH'></tfoot>
              • <legend id='CWOqH'><style id='CWOqH'><dir id='CWOqH'><q id='CWOqH'></q></dir></style></legend>

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

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

                  <bdo id='CWOqH'></bdo><ul id='CWOqH'></ul>
                • 本文介绍了将 100% 的内核与多处理模块一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有两段代码用于了解 Python 3.1 中的多处理.我的目标是使用 100% 的所有可用处理器.但是,这里的代码片段在所有处理器上仅达到 30% - 50%.

                  I have two pieces of code that I'm using to learn about multiprocessing in Python 3.1. My goal is to use 100% of all the available processors. However, the code snippets here only reach 30% - 50% on all processors.

                  无论如何强制"python 100% 使用?操作系统(Windows 7、64 位)是否限制了 Python 对处理器的访问?当下面的代码片段正在运行时,我打开任务管理器并观察处理器的峰值,但从未达到并保持 100%.除此之外,我还可以看到在此过程中创建和销毁了多个 python.exe 进程.这些过程与处理器有什么关系?例如,如果我生成 4 个进程,则每个进程都没有使用它自己的核心.相反,这些进程使用的是什么?他们是否共享所有内核?如果是这样,是操作系统强制进程共享内核吗?

                  Is there anyway to 'force' python to use all 100%? Is the OS (windows 7, 64bit) limiting Python's access to the processors? While the code snippets below are running, I open the task manager and watch the processor's spike, but never reach and maintain 100%. In addition to that, I can see multiple python.exe processes created and destroyed along the way. How do these processes relate to processors? For example, if I spawn 4 processes, each process isn't using it's own core. Instead, what are the processes using? Are they sharing all cores? And if so, is it the OS that is forcing the processes to share the cores?

                  import multiprocessing
                  
                  def worker():
                      #worker function
                      print ('Worker')
                      x = 0
                      while x < 1000:
                          print(x)
                          x += 1
                      return
                  
                  if __name__ == '__main__':
                      jobs = []
                      for i in range(50):
                          p = multiprocessing.Process(target=worker)
                          jobs.append(p)
                          p.start()
                  

                  代码片段 2

                  from multiprocessing import Process, Lock
                  
                  def f(l, i):
                      l.acquire()
                      print('worker ', i)
                      x = 0
                      while x < 1000:
                          print(x)
                          x += 1
                      l.release()
                  
                  if __name__ == '__main__': 
                      lock = Lock()
                      for num in range(50):
                          Process(target=f, args=(lock, num)).start()
                  

                  推荐答案

                  要使用 100% 的所有内核,不要创建和销毁新进程.

                  To use 100% of all cores, do not create and destroy new processes.

                  为每个核心创建几个进程并将它们与管道链接.

                  Create a few processes per core and link them with a pipeline.

                  在操作系统级别,所有流水线进程同时运行.

                  At the OS-level, all pipelined processes run concurrently.

                  你写的越少(你委托给操作系统的越多)你就越有可能使用尽可能多的资源.

                  The less you write (and the more you delegate to the OS) the more likely you are to use as many resources as possible.

                  python p1.py | python p2.py | python p3.py | python p4.py ...
                  

                  将最大限度地利用您的 CPU.

                  Will make maximal use of your CPU.

                  这篇关于将 100% 的内核与多处理模块一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 3 中的多处理、多线程和异步 下一篇:如何使用python的多处理终止进程

                  相关文章

                  最新文章

                  <tfoot id='Sd9jW'></tfoot>

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

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

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