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

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

    <small id='9MyXP'></small><noframes id='9MyXP'>

        <i id='9MyXP'><tr id='9MyXP'><dt id='9MyXP'><q id='9MyXP'><span id='9MyXP'><b id='9MyXP'><form id='9MyXP'><ins id='9MyXP'></ins><ul id='9MyXP'></ul><sub id='9MyXP'></sub></form><legend id='9MyXP'></legend><bdo id='9MyXP'><pre id='9MyXP'><center id='9MyXP'></center></pre></bdo></b><th id='9MyXP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='9MyXP'><tfoot id='9MyXP'></tfoot><dl id='9MyXP'><fieldset id='9MyXP'></fieldset></dl></div>
          <bdo id='9MyXP'></bdo><ul id='9MyXP'></ul>
      1. 關于使用 Queue()/deque() 和類變量進行通信和“毒丸

        Process vs. Thread with regards to using Queue()/deque() and class variable for communication and quot;poison pillquot;(關于使用 Queue()/deque() 和類變量進行通信和“毒丸的進程與線程) - IT屋-程序員軟件開發技術分
        <i id='DmR1n'><tr id='DmR1n'><dt id='DmR1n'><q id='DmR1n'><span id='DmR1n'><b id='DmR1n'><form id='DmR1n'><ins id='DmR1n'></ins><ul id='DmR1n'></ul><sub id='DmR1n'></sub></form><legend id='DmR1n'></legend><bdo id='DmR1n'><pre id='DmR1n'><center id='DmR1n'></center></pre></bdo></b><th id='DmR1n'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='DmR1n'><tfoot id='DmR1n'></tfoot><dl id='DmR1n'><fieldset id='DmR1n'></fieldset></dl></div>
          <bdo id='DmR1n'></bdo><ul id='DmR1n'></ul>

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

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

                1. 本文介紹了關于使用 Queue()/deque() 和類變量進行通信和“毒丸"的進程與線程的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我想創建一個在 While True 循環中永遠運行的線程或進程.

                  I would like to create either a Thread or a Process which runs forever in a While True loop.

                  我需要以隊列的形式向工作人員發送和接收數據,無論是 multiprocessing.Queue() 還是 collections.deque().我更喜歡使用 collections.deque(),因為它明顯更快.

                  I need to send and receive data to the worker in the form for queues, either a multiprocessing.Queue() or a collections.deque(). I prefer to use collections.deque() as it is significantly faster.

                  我還需要最終能夠殺死工作人員(因為它在 while True 循環中運行.這是我整理的一些測試代碼,以嘗試了解線程、進程、隊列和 deque 之間的區別..

                  I also need to be able to kill the worker eventually (as it runs in a while True loop. Here is some test code I've put together to try and understand the differences between Threads, Processes, Queues, and deque ..

                  import time
                  from multiprocessing import Process, Queue
                  from threading import Thread
                  from collections import deque
                  
                  class ThreadingTest(Thread):
                  
                      def __init__(self, q):
                          super(ThreadingTest, self).__init__()
                          self.q = q
                          self.toRun = False
                  
                      def run(self):
                          print("Started Thread")
                          self.toRun = True
                          while self.toRun:
                              if type(self.q) == type(deque()):
                                  if self.q:
                                      i = self.q.popleft()
                                      print("Thread deque: " + str(i))
                              elif type(self.q) == type(Queue()):
                                  if not self.q.empty():
                                      i = self.q.get_nowait()
                                      print("Thread Queue: " + str(i))
                  
                      def stop(self):
                          print("Trying to stop Thread")
                          self.toRun = False
                          while self.isAlive():
                              time.sleep(0.1)
                          print("Stopped Thread")
                  
                  class ProcessTest(Process):
                  
                      def __init__(self, q):
                          super(ProcessTest, self).__init__()
                          self.q = q
                          self.toRun = False
                          self.ctr = 0
                  
                      def run(self):
                          print("Started Process")
                          self.toRun = True
                          while self.toRun:
                              if type(self.q) == type(deque()):
                                  if self.q:
                                      i = self.q.popleft()
                                      print("Process deque: " + str(i))
                              elif type(self.q) == type(Queue()):
                                  if not self.q.empty():
                                      i = self.q.get_nowait()
                                      print("Process Queue: " + str(i))
                  
                      def stop(self):
                          print("Trying to stop Process")
                          self.toRun = False
                          while self.is_alive():
                              time.sleep(0.1)
                          print("Stopped Process")
                  
                  if __name__ == '__main__':
                      q = Queue()
                      t1 = ProcessTest(q)
                      t1.start()
                  
                      for i in range(10):
                          if type(q) == type(deque()):
                              q.append(i)
                          elif type(q) == type(Queue()):
                              q.put_nowait(i)
                          time.sleep(1)
                      t1.stop()
                      t1.join()
                  
                      if type(q) == type(deque()):
                          print(q)
                      elif type(q) == type(Queue()):
                          while q.qsize() > 0:
                              print(str(q.get_nowait()))
                  

                  如您所見,t1 可以是 ThreadingTest 或 ProcessTest.此外,傳遞給它的隊列可以是 multiprocessing.Queue 或 collections.deque.

                  As you can see, t1 can either be ThreadingTest, or ProcessTest. Also, the queue passed to it can either be a multiprocessing.Queue or a collections.deque.

                  ThreadingTest 與 Queue 或 deque() 一起使用.當調用 stop() 方法時,它也會正確終止 run().

                  ThreadingTest works with a Queue or deque(). It also kills run() properly when the stop() method is called.

                  Started Thread
                  Thread deque: 0
                  Thread deque: 1
                  Thread deque: 2
                  Thread deque: 3
                  Thread deque: 4
                  Thread deque: 5
                  Thread deque: 6
                  Thread deque: 7
                  Thread deque: 8
                  Thread deque: 9
                  Trying to stop Thread
                  Stopped Thread
                  deque([])
                  

                  ProcessTest 只有在隊列類型為 multiprocessing.Queue 時才能從隊列中讀取.它不適用于 collections.deque.此外,我無法使用 stop() 終止進程.

                  ProcessTest is only able to read from the queue if it is of type multiprocessing.Queue. It doesn't work with collections.deque. Furthermore, I am unable to kill the process using stop().

                  Process Queue: 0
                  Process Queue: 1
                  Process Queue: 2
                  Process Queue: 3
                  Process Queue: 4
                  Process Queue: 5
                  Process Queue: 6
                  Process Queue: 7
                  Process Queue: 8
                  Process Queue: 9
                  Trying to stop Process
                  

                  我想知道為什么?另外,在進程中使用雙端隊列的最佳方法是什么?而且,我將如何使用某種 stop() 方法來終止進程.

                  I'm trying to figure out why? Also, what would be the best way to use deque with a process? And, how would I go about killing the process using some sort of stop() method.

                  推薦答案

                  你不能使用 collections.deque 在兩個 multiprocessing.Process 實例之間傳遞數據,因為 collections.deque 不是進程感知的.multiprocessing.Queue 在內部將其內容寫入 multiprocessing.Pipe,這意味著其中的數據可以在一個進程中排隊并在另一個進程中檢索.collections.deque 沒有那種管道,所以它不會工作.當您在一個進程中寫入 deque 時,另一個進程中的 deque 實例完全不會受到影響;它們是完全獨立的實例.

                  You can't use a collections.deque to pass data between two multiprocessing.Process instances, because collections.deque is not process-aware. multiprocessing.Queue writes its contents to a multiprocessing.Pipe internally, which means that data in it can be enqueued in once process and retrieved in another. collections.deque doesn't have that kind of plumbing, so it won't work. When you write to the deque in one process, the deque instance in the other process won't be affected at all; they're completely separate instances.

                  您的 stop() 方法也發生了類似的問題.您正在主進程中更改 toRun 的值,但這根本不會影響子進程.它們是完全獨立的實例.結束孩子的最好方法是向 Queue 發送一些哨兵.當你在child中獲得哨兵時,跳出無限循環:

                  A similar issue is happening to your stop() method. You're changing the value of toRun in the main process, but this won't affect the child at all. They're completely separate instances. The best way to end the child would be to send some sentinel to the Queue. When you get the sentinel in the child, break out of the infinite loop:

                  def run(self):
                      print("Started Process")
                      self.toRun = True
                      while self.toRun:
                          if type(self.q) == type(deque()):
                              if self.q:
                                  i = self.q.popleft()
                                  print("Process deque: " + str(i))
                          elif type(self.q) == type(Queue()):
                              if not self.q.empty():
                                  i = self.q.get_nowait()
                                  if i is None:  
                                      break  # Got sentinel, so break
                                  print("Process Queue: " + str(i))
                  
                  def stop(self):
                      print("Trying to stop Process")
                      self.q.put(None)  # Send sentinel
                      while self.is_alive():
                          time.sleep(0.1)
                      print("Stopped Process")
                  

                  如果你確實需要兩個進程之間的 deque 語義,你可以使用 自定義 multiprocessing.Manager() 以在 Manager 進程中創建共享 deque,以及您的每個 Process 實例都會獲得一個 Proxy:

                  If you actually do need deque semantics between two process, you can use a custom multiprocessing.Manager() to create a shared deque in a Manager process, and each of your Process instances will get a Proxy to it:

                  import time
                  from multiprocessing import Process
                  from multiprocessing.managers import SyncManager
                  from collections import deque
                  
                  SyncManager.register('deque', deque)
                  
                  def Manager():
                      m = SyncManager()
                      m.start()
                      return m
                  
                  class ProcessTest(Process):
                      def __init__(self, q):
                          super(ProcessTest, self).__init__()
                          self.q = q
                          self.ctr = 0
                  
                      def run(self):
                          print("Started Process")
                          self.toRun = True
                          while self.toRun:
                              if self.q._getvalue():
                                  i = self.q.popleft()
                                  if i is None:
                                      break
                                  print("Process deque: " + str(i))
                  
                      def stop(self):
                          print("Trying to stop Process")
                          self.q.append(None)
                          while self.is_alive():
                              time.sleep(0.1)
                          print("Stopped Process")
                  
                  if __name__ == '__main__':
                      m = Manager()
                      q = m.deque()
                      t1 = ProcessTest(q)
                      t1.start()
                  
                      for i in range(10):
                          q.append(i)
                          time.sleep(1)
                      t1.stop()
                      t1.join()
                  
                      print(q)
                  

                  請注意,這可能不會比 multiprocessing.Queue 快,因為每次訪問 deque 都會產生 IPC 成本.對于以您的方式傳遞消息來說,它也是一種不太自然的數據結構.

                  Note that this probably isn't going to be faster than a multiprocessing.Queue, though, since there's an IPC cost for every time you access the deque. It's also a much less natural data structure for passing messages the way you are.

                  這篇關于關于使用 Queue()/deque() 和類變量進行通信和“毒丸"的進程與線程的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                2. <legend id='oZkrz'><style id='oZkrz'><dir id='oZkrz'><q id='oZkrz'></q></dir></style></legend>

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

                      <bdo id='oZkrz'></bdo><ul id='oZkrz'></ul>
                        <tbody id='oZkrz'></tbody>
                        1. <small id='oZkrz'></small><noframes id='oZkrz'>

                            主站蜘蛛池模板: 欧美日韩国产精品一区二区 | 国产精品自产av一区二区三区 | 国产亚洲一区二区三区 | 精品福利视频一区二区三区 | 日日干夜夜操天天操 | 成人国产在线观看 | jlzzjlzz国产精品久久 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 草久久久 | 阿v视频在线观看 | 日韩欧美专区 | 黄色免费av | 欧美一区二区三区在线看 | 国产精品国产a级 | 成人午夜免费福利视频 | 国产精品日本一区二区在线播放 | 久久久www | 亚洲 成人 av | 91精品国产色综合久久 | 懂色中文一区二区三区在线视频 | www.久久久.com | 91精品国产自产在线老师啪 | 四虎影院在线观看av | 在线精品亚洲欧美日韩国产 | 日韩精品中文字幕在线 | 欧美一区二区三区久久精品 | 国产精品久久久久久久久久99 | 在线āv视频 | 国产精品免费一区二区三区 | 特黄毛片 | 免费在线观看黄色av | 天天操夜夜骑 | 久久伊人精品一区二区三区 | 色在线免费 | 精品视频一区二区 | 欧美最猛黑人 | 国产一区视频在线 | 一级大黄 | 91新视频| 男女爱爱福利视频 | 九九成人 |