久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

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

        <tfoot id='SsmMD'></tfoot>

        python多處理的生產(chǎn)者/消費(fèi)者問(wèn)題

        producer/consumer problem with python multiprocessing(python多處理的生產(chǎn)者/消費(fèi)者問(wèn)題)

        <legend id='6nzvl'><style id='6nzvl'><dir id='6nzvl'><q id='6nzvl'></q></dir></style></legend>

          <i id='6nzvl'><tr id='6nzvl'><dt id='6nzvl'><q id='6nzvl'><span id='6nzvl'><b id='6nzvl'><form id='6nzvl'><ins id='6nzvl'></ins><ul id='6nzvl'></ul><sub id='6nzvl'></sub></form><legend id='6nzvl'></legend><bdo id='6nzvl'><pre id='6nzvl'><center id='6nzvl'></center></pre></bdo></b><th id='6nzvl'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6nzvl'><tfoot id='6nzvl'></tfoot><dl id='6nzvl'><fieldset id='6nzvl'></fieldset></dl></div>
          • <bdo id='6nzvl'></bdo><ul id='6nzvl'></ul>
          • <tfoot id='6nzvl'></tfoot>
              <tbody id='6nzvl'></tbody>

              <small id='6nzvl'></small><noframes id='6nzvl'>

                  本文介紹了python多處理的生產(chǎn)者/消費(fèi)者問(wèn)題的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我正在編寫(xiě)一個(gè)有一個(gè)生產(chǎn)者和多個(gè)消費(fèi)者的服務(wù)器程序,讓我感到困惑的是只有第一個(gè)放入隊(duì)列的任務(wù)生產(chǎn)者得到消耗,之后排隊(duì)的任務(wù)不再被消耗,它們?nèi)匀淮嬖谟肋h(yuǎn)在隊(duì)列中.

                  I am writing a server program with one producer and multiple consumers, what confuses me is only the first task producer put into the queue gets consumed, after which tasks enqueued no longer get consumed, they remain in the queue forever.

                  from multiprocessing import Process, Queue, cpu_count
                  from http import httpserv
                  import time
                  
                  def work(queue):
                      while True:
                          task = queue.get()
                          if task is None:
                              break
                          time.sleep(5)
                          print "task done:", task
                      queue.put(None)
                  
                  class Manager:
                      def __init__(self):
                          self.queue = Queue()
                          self.NUMBER_OF_PROCESSES = cpu_count()
                  
                      def start(self):
                          self.workers = [Process(target=work, args=(self.queue,))
                                          for i in xrange(self.NUMBER_OF_PROCESSES)]
                          for w in self.workers:
                              w.start()
                  
                          httpserv(self.queue)
                  
                      def stop(self):
                          self.queue.put(None)
                          for i in range(self.NUMBER_OF_PROCESSES):
                              self.workers[i].join()
                          queue.close()
                  
                  Manager().start()
                  

                  生產(chǎn)者是一個(gè) HTTP 服務(wù)器,一旦接收到任務(wù),它就會(huì)將任務(wù)放入隊(duì)列中來(lái)自用戶(hù)的請(qǐng)求.看來(lái)消費(fèi)者流程還在當(dāng)隊(duì)列中有新任務(wù)時(shí)阻塞,這很奇怪.

                  The producer is a HTTP server which put a task in the queue once receive a request from the user. It seems that consumer processes are still blocked when there are new tasks in the queue, which is weird.

                  附:另外兩個(gè)與上述無(wú)關(guān)的問(wèn)題,我不確定是否最好將 HTTP 服務(wù)器放在自己的進(jìn)程中而不是主進(jìn)程中進(jìn)程,如果是,我怎樣才能讓主進(jìn)程繼續(xù)運(yùn)行子進(jìn)程結(jié)束.第二個(gè)問(wèn)題,什么是最好的方法來(lái)阻止HTTP 服務(wù)器優(yōu)雅嗎?

                  P.S. Another two questions not relating to the above, I am not sure if it's better to put HTTP server in its own process other than the main process, if yes how can I make the main process keep running before all children processes end. Second question, what's the best way to stop the HTTP server gracefully?

                  編輯:添加生產(chǎn)者代碼,它只是一個(gè)簡(jiǎn)單的python wsgi服務(wù)器:

                  Edit: add producer code, it's just a simple python wsgi server:

                  import fapws._evwsgi as evwsgi
                  from fapws import base
                  
                  def httpserv(queue):
                      evwsgi.start("0.0.0.0", 8080)
                      evwsgi.set_base_module(base)
                  
                      def request_1(environ, start_response):
                          start_response('200 OK', [('Content-Type','text/html')])
                          queue.put('task_1')
                          return ["request 1!"]
                  
                      def request_2(environ, start_response):
                          start_response('200 OK', [('Content-Type','text/html')])
                          queue.put('task_2')
                          return ["request 2!!"]
                  
                      evwsgi.wsgi_cb(("/request_1", request_1))
                      evwsgi.wsgi_cb(("/request_2", request_2))
                  
                      evwsgi.run()
                  

                  推薦答案

                  我認(rèn)為 Web 服務(wù)器部分一定有問(wèn)題,因?yàn)樗\(yùn)行良好:

                  I think there must be something wrong with the web server part, as this works perfectly:

                  from multiprocessing import Process, Queue, cpu_count
                  import random
                  import time
                  
                  
                  def serve(queue):
                      works = ["task_1", "task_2"]
                      while True:
                          time.sleep(0.01)
                          queue.put(random.choice(works))
                  
                  
                  def work(id, queue):
                      while True:
                          task = queue.get()
                          if task is None:
                              break
                          time.sleep(0.05)
                          print "%d task:" % id, task
                      queue.put(None)
                  
                  
                  class Manager:
                      def __init__(self):
                          self.queue = Queue()
                          self.NUMBER_OF_PROCESSES = cpu_count()
                  
                      def start(self):
                          print "starting %d workers" % self.NUMBER_OF_PROCESSES
                          self.workers = [Process(target=work, args=(i, self.queue,))
                                          for i in xrange(self.NUMBER_OF_PROCESSES)]
                          for w in self.workers:
                              w.start()
                  
                          serve(self.queue)
                  
                      def stop(self):
                          self.queue.put(None)
                          for i in range(self.NUMBER_OF_PROCESSES):
                              self.workers[i].join()
                          self.queue.close()
                  
                  
                  Manager().start()
                  

                  樣本輸出:

                  starting 2 workers
                  0 task: task_1
                  1 task: task_2
                  0 task: task_2
                  1 task: task_1
                  0 task: task_1
                  

                  這篇關(guān)于python多處理的生產(chǎn)者/消費(fèi)者問(wèn)題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個(gè)參數(shù)傳遞給 pool.map() 函數(shù))
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開(kāi)
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進(jìn)程池.當(dāng)其中一個(gè)工作進(jìn)程確定不再需要完成工作時(shí),如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊(duì)列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯(cuò)誤的另一個(gè)混淆,“模塊對(duì)象沒(méi)有屬性“f)
                    <tfoot id='sVuLV'></tfoot>
                  1. <i id='sVuLV'><tr id='sVuLV'><dt id='sVuLV'><q id='sVuLV'><span id='sVuLV'><b id='sVuLV'><form id='sVuLV'><ins id='sVuLV'></ins><ul id='sVuLV'></ul><sub id='sVuLV'></sub></form><legend id='sVuLV'></legend><bdo id='sVuLV'><pre id='sVuLV'><center id='sVuLV'></center></pre></bdo></b><th id='sVuLV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sVuLV'><tfoot id='sVuLV'></tfoot><dl id='sVuLV'><fieldset id='sVuLV'></fieldset></dl></div>
                      <tbody id='sVuLV'></tbody>

                            <bdo id='sVuLV'></bdo><ul id='sVuLV'></ul>
                            <legend id='sVuLV'><style id='sVuLV'><dir id='sVuLV'><q id='sVuLV'></q></dir></style></legend>
                          • <small id='sVuLV'></small><noframes id='sVuLV'>

                            主站蜘蛛池模板: 日韩网站在线观看 | 国产一区二区三区四区在线观看 | 男女黄网站| 搞黄网站在线观看 | 欧美aaaa视频| 99热在线免费 | 久久久xx | 欧美成人第一页 | 久久中文字幕视频 | aaaaaa大片免费看最大的 | 在线观看免费高清av | 91欧美| 国产激情一区二区三区 | 久久精品国产一区二区三区不卡 | 日韩有码一区 | 国产成人福利 | 国产精品国产成人国产三级 | 国产91久久久久蜜臀青青天草二 | 国产三级 | 在线观看视频中文字幕 | 一区二区精品 | 日本高清中文字幕 | 国产一二区视频 | 国产精品视频一区二区三区四蜜臂 | 日韩久久久久久 | 干干干操操操 | 久久狠狠 | 日韩成人影院在线观看 | 国产一区二区毛片 | 久久狠狠 | 欧美一级视频免费看 | 久综合| 在线亚洲一区 | 久久久一区二区三区四区 | 日韩免费高清视频 | 成人高清在线 | 亚洲欧美在线观看 | 亚洲精品1区 | 久久另类| 欧洲一级毛片 | 91免费看片 |