• <small id='EpdlS'></small><noframes id='EpdlS'>

    <legend id='EpdlS'><style id='EpdlS'><dir id='EpdlS'><q id='EpdlS'></q></dir></style></legend>

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

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

        带有工作进程的 python 池

        时间:2023-05-26
          <tfoot id='hMVQs'></tfoot>
            <tbody id='hMVQs'></tbody>

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

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

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

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

                • 本文介绍了带有工作进程的 python 池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用进程对象在 python 中使用工作池.每个工人(一个进程)进行一些初始化(花费大量时间),传递一系列作业(理想情况下使用 map()),并返回一些东西.除此之外,不需要任何沟通.但是,我似乎无法弄清楚如何使用 map() 来使用我的工人的 compute() 函数.

                  I am trying to use a worker Pool in python using Process objects. Each worker (a Process) does some initialization (takes a non-trivial amount of time), gets passed a series of jobs (ideally using map()), and returns something. No communication is necessary beyond that. However, I can't seem to figure out how to use map() to use my worker's compute() function.

                  from multiprocessing import Pool, Process
                  
                  class Worker(Process):
                      def __init__(self):
                          print 'Worker started'
                          # do some initialization here
                          super(Worker, self).__init__()
                  
                      def compute(self, data):
                          print 'Computing things!'
                          return data * data
                  
                  if __name__ == '__main__':
                      # This works fine
                      worker = Worker()
                      print worker.compute(3)
                  
                      # workers get initialized fine
                      pool = Pool(processes = 4,
                                  initializer = Worker)
                      data = range(10)
                      # How to use my worker pool?
                      result = pool.map(compute, data)
                  

                  是作业队列代替,还是我可以使用 map()?

                  Is a job queue the way to go instead, or can I use map()?

                  推荐答案

                  我建议你为此使用队列.

                  I would suggest that you use a Queue for this.

                  class Worker(Process):
                      def __init__(self, queue):
                          super(Worker, self).__init__()
                          self.queue = queue
                  
                      def run(self):
                          print('Worker started')
                          # do some initialization here
                  
                          print('Computing things!')
                          for data in iter(self.queue.get, None):
                              # Use data
                  

                  现在您可以开始一堆这些,所有这些都从一个队列中获取工作

                  Now you can start a pile of these, all getting work from a single queue

                  request_queue = Queue()
                  for i in range(4):
                      Worker(request_queue).start()
                  for data in the_real_source:
                      request_queue.put(data)
                  # Sentinel objects to allow clean shutdown: 1 per worker.
                  for i in range(4):
                      request_queue.put(None) 
                  

                  这样的事情应该可以让您将昂贵的启动成本分摊给多个工人.

                  That kind of thing should allow you to amortize the expensive startup cost across multiple workers.

                  这篇关于带有工作进程的 python 池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:了解多处理:Python 中的共享内存管理、锁和队列 下一篇:Python:在使用多处理池时使用队列写入单个文件

                  相关文章

                  最新文章

                      <legend id='8Y53R'><style id='8Y53R'><dir id='8Y53R'><q id='8Y53R'></q></dir></style></legend>
                      • <bdo id='8Y53R'></bdo><ul id='8Y53R'></ul>

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

                    1. <small id='8Y53R'></small><noframes id='8Y53R'>

                    2. <tfoot id='8Y53R'></tfoot>