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

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

        python线程-迭代n个任务时总是有x个活动线程

        时间:2023-09-29
            <i id='Ym8fV'><tr id='Ym8fV'><dt id='Ym8fV'><q id='Ym8fV'><span id='Ym8fV'><b id='Ym8fV'><form id='Ym8fV'><ins id='Ym8fV'></ins><ul id='Ym8fV'></ul><sub id='Ym8fV'></sub></form><legend id='Ym8fV'></legend><bdo id='Ym8fV'><pre id='Ym8fV'><center id='Ym8fV'></center></pre></bdo></b><th id='Ym8fV'></th></span></q></dt></tr></i><div id='Ym8fV'><tfoot id='Ym8fV'></tfoot><dl id='Ym8fV'><fieldset id='Ym8fV'></fieldset></dl></div>

                <tbody id='Ym8fV'></tbody>
            1. <tfoot id='Ym8fV'></tfoot>
                <legend id='Ym8fV'><style id='Ym8fV'><dir id='Ym8fV'><q id='Ym8fV'></q></dir></style></legend>

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

                  <bdo id='Ym8fV'></bdo><ul id='Ym8fV'></ul>
                • 本文介绍了python线程-迭代n个任务时总是有x个活动线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我基本上想做的是以下几点:

                  What I basically want to do is the following:

                  import threading
                  import Queue
                  
                  def test_thread(elem, q):
                      q.put(elem ** 2)
                  
                  a = [1,2,3,4,5,6,7,8]
                  q = Queue.Queue()
                  results = []
                  for x in range(8):
                      print x
                      threading.Thread(target=test_thread, args=(a[x], q)).start()
                      results.append(q.get())
                  

                  但不是一次运行所有线程,我只想并行运行 2 个线程并迭代列表.一旦一个线程完成,就应该处理列表中的下一个值.我找不到示例,也不确定如何为此构建循环.

                  But instead of running all threads at once I want to run only say 2 in parallel adn iterate over the list. Once one thread is done the next value from the list should be processed. I could't find an example and I am not sure how to construct the loops for this.

                  另外,我不明白队列的行为.我本来希望所有平方数都在队列中.但相反,只有一个值?(上面的代码已更改为将所有结果存储在结果"中).非常感谢提示、评论、关键字.

                  In addition, I do not understand the behavior of the Queue. I would have expected that ALL squared numbers are in the queue. But instead there is only one value? (The code above has changed to store all results in "results"). Hints, comments, keywords are highly appreciated.

                  第二个问题:

                  抱歉,我以为 q.get() 会返回所有结果.但它只是像时尚一样给出队列中的元素.

                  Sorry, I thought q.get() will return all the results. But it just gives the elements in a queue like fashion.

                  推荐答案

                  你可以为此使用线程池:

                  You can use a thread pool for this:

                  import threading
                  from multiprocessing.pool import ThreadPool
                  
                  def test_thread(elem):
                      return elem ** 2
                  
                  a = [1,2,3,4,5,6,7,8]
                  pool = ThreadPool(2) # 2 worker threads
                  results = []
                  for x in range(8):
                      print x
                      results.append(pool.apply_async(test_thread, args=(a[x],)))
                  
                  results = [result.get() for result in results]
                  # You can also replace this for loop altogether using pool.map
                  # and get the same result:
                  # results = pool.map(test_thread, range(8))
                  print(results)
                  

                  输出:

                  0
                  1
                  2
                  3
                  4
                  5
                  6
                  7
                  [1, 4, 9, 16, 25, 36, 49, 64]
                  

                  ThreadPool 类是 <代码>多处理模块.它也可以通过 multiprocessing.dummy.Pool.它允许您创建一个线程池来处理任意数量的工作项,同时始终将同时处理的工作项的数量限制为您指定的数量.您可以使用普通 multiprocessing.Pool 来了解它的 API.完全一样,只是到处都是进程",你用线程"替换它.

                  The ThreadPool class is a mostly undocumented part of the multiprocessing module. It can also be access via multiprocessing.dummy.Pool. It allows you to create a pool of threads to handle any number of work items, while always limiting the number of work items being concurrently processed to something you specify. You can use the documentation for the normal multiprocessing.Pool to learn about its API. It's exactly the same, except everywhere it says "process", you replace it with "thread".

                  我不确定我是否遵循您关于 Queue.Queue 的问题的第二部分.在 for 循环的每次迭代中,您将一个项目放入 test_thread 内的 Queue 中,然后使用 results.append(q.get()).因此,虽然 Queue 中的项目一次不会超过一项,但它被用于传输最终出现在 results 列表中的所有值 - 每个值一个range(8) 列表中的项目.

                  I'm not sure I follow the second part of your question about Queue.Queue. On each iteration of your for loop, you're putting one item into the Queue inside test_thread, and then consuming it inside the for loop using results.append(q.get()). So while there is never more than one item in the Queue at a time, it is being used to transfer all the values that end up in the results list - one for each item in the range(8) list.

                  这篇关于python线程-迭代n个任务时总是有x个活动线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='ukoKn'></bdo><ul id='ukoKn'></ul>
                    <tfoot id='ukoKn'></tfoot>
                      <legend id='ukoKn'><style id='ukoKn'><dir id='ukoKn'><q id='ukoKn'></q></dir></style></legend>

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

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

                            <tbody id='ukoKn'></tbody>