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

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

    <tfoot id='qi1tU'></tfoot>

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

      如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?

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

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

              • <legend id='Oedv9'><style id='Oedv9'><dir id='Oedv9'><q id='Oedv9'></q></dir></style></legend>
              • <small id='Oedv9'></small><noframes id='Oedv9'>

                本文介绍了如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用简单的线程模块来执行并发作业.现在我想利用并发期货模块.有人能给我举一个使用队列和并发库的例子吗?

                I am using simple threading modules to do concurrent jobs. Now I would like to take advantages of concurrent futures modules. Can some put me a example of using a queue with concurrent library?

                我收到 TypeError: 'Queue' object is not iterable我不知道如何迭代队列

                I am getting TypeError: 'Queue' object is not iterable I dont know how to iterate queues

                代码片段:

                 def run(item):
                      self.__log.info(str(item))
                      return True
                <queue filled here>
                
                with concurrent.futures.ThreadPoolExecutor(max_workers = 100) as executor:
                        furtureIteams = { executor.submit(run, item): item for item in list(queue)}
                        for future in concurrent.futures.as_completed(furtureIteams):
                            f = furtureIteams[future]
                            print(f)
                

                推荐答案

                我会建议这样的事情:

                def run(queue):
                      item = queue.get()
                      self.__log.info(str(item))
                      return True
                <queue filled here>
                workerThreadsToStart = 10
                with concurrent.futures.ThreadPoolExecutor(max_workers = 100) as executor:
                        furtureIteams = { executor.submit(run, queue): index for intex in range(workerThreadsToStart)}
                        for future in concurrent.futures.as_completed(furtureIteams):
                            f = furtureIteams[future]
                            print(f)
                

                您将遇到的问题是,队列被认为是无止境的,并且作为一种媒介来解耦将某些内容放入队列的线程和将项目从队列中取出的线程.

                The problem you will run in is that a queue is thought to be endless and as a medium to decouple the threads that put something into the queue and threads that get items out of the queue.

                1. 您的商品数量有限或
                2. 您一次计算所有项目

                然后并行处理它们,队列没有意义.在这些情况下,ThreadPoolExecutor 会使队列过时.

                and afterwards process them in parallel, a queue makes no sense. A ThreadPoolExecutor makes a queue obsolete in these cases.

                我查看了 ThreadPoolExecutor 源代码:

                I had a look at the ThreadPoolExecutor source:

                def submit(self, fn, *args, **kwargs): # line 94
                    self._work_queue.put(w) # line 102
                

                里面使用了一个队列.

                这篇关于如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:multiprocessing.Manager 嵌套共享对象不适用于队列 下一篇:将复杂字典放入返回队列时,多处理进程不加入

                相关文章

                    <bdo id='ty4Od'></bdo><ul id='ty4Od'></ul>

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

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