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

  • <tfoot id='04AZy'></tfoot>

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

      • <bdo id='04AZy'></bdo><ul id='04AZy'></ul>
    1. <small id='04AZy'></small><noframes id='04AZy'>

      <legend id='04AZy'><style id='04AZy'><dir id='04AZy'><q id='04AZy'></q></dir></style></legend>

        在 python 服務器中的進程之間共享列表

        Share list between process in python server(在 python 服務器中的進程之間共享列表)
            <tbody id='alRJE'></tbody>

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

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

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

                <i id='alRJE'><tr id='alRJE'><dt id='alRJE'><q id='alRJE'><span id='alRJE'><b id='alRJE'><form id='alRJE'><ins id='alRJE'></ins><ul id='alRJE'></ul><sub id='alRJE'></sub></form><legend id='alRJE'></legend><bdo id='alRJE'><pre id='alRJE'><center id='alRJE'></center></pre></bdo></b><th id='alRJE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='alRJE'><tfoot id='alRJE'></tfoot><dl id='alRJE'><fieldset id='alRJE'></fieldset></dl></div>
                  本文介紹了在 python 服務器中的進程之間共享列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我有一個簡單的 UDPServer,它與 multiprocessing 一起工作.

                  I have simple UDPServer, which works with multiprocessing.

                  我想創建一個包含所有客戶信息的列表.

                  I want to create a list, that contains information about all clients.

                  我使用Manager,但我不明白,如何在列表中追加信息 - 我需要轉移Manager的對象來處理,但是如何?我使用新屬性的方式不起作用.

                  I use Manager, but I don't understand, how to append information in list - I need transfer Manager`s object to handle, but how? My way with new attribute does not work.

                  import multiprocessing
                  from socketserver import UDPServer, ForkingMixIn, DatagramRequestHandler
                  from socket import socket, AF_INET, SOCK_DGRAM
                  from settings import host, port, number_of_connections
                  
                  class ChatHandler(DatagramRequestHandler):
                  
                      def handle(self):
                          cur_process = multiprocessing.current_process()
                          data = self.request[0].strip()
                          socket = self.request[1]
                          ChatHandler.clients.append(self.client_address) # error here
                          print(ChatHandler.clients)
                  
                  
                  class ChatServer(ForkingMixIn, UDPServer):
                      pass
                  
                  
                  if __name__ == '__main__':
                      server = ChatServer((host, port), ChatHandler)
                      ChatHandler.clients = multiprocessing.Manager().list()
                      server_process = multiprocessing.Process(target=server.serve_forever)
                      server_process.daemon = False
                      server_process.start()
                  

                  如何解決這個問題?謝謝!

                  How to fix that? Thanks!

                  輸出:

                  Exception happened during processing of request from ('127.0.0.1', 55679)
                  Traceback (most recent call last):
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/managers.py", line 724, in _callmethod
                      conn = self._tls.connection
                  AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
                  
                  During handling of the above exception, another exception occurred:
                  
                  Traceback (most recent call last):
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 584, in process_request
                      self.finish_request(request, client_address)
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 344, in finish_request
                      self.RequestHandlerClass(request, client_address, self)
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socketserver.py", line 665, in __init__
                      self.handle()
                    File "server.py", line 15, in handle
                      ChatHandler.clients.append(self.client_address)
                    File "<string>", line 2, in append
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/managers.py", line 728, in _callmethod
                      self._connect()
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/managers.py", line 715, in _connect
                      conn = self._Client(self._token.address, authkey=self._authkey)
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/connection.py", line 495, in Client
                      c = SocketClient(address)
                    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/connection.py", line 624, in SocketClient
                      s.connect(address)
                  FileNotFoundError: [Errno 2] No such file or directory
                  

                  推薦答案

                  問題是你讓主進程在你啟動工作進程后立即完成它的執行.當創建 multiprocessing.Manager 的進程完成執行時,Manager 服務器將關閉,這意味著您的共享列表對象現在已無用.發生這種情況是因為 Manager 對象將它的 shutdown 函數注冊為 multiprocessing 模塊的終結器",這意味著它將在進程退出.這是在 BaseManager.__init__ 中注冊它的代碼:

                  The problem is that you're letting the main process finish its execution immediately after you start the worker process. When the process that created the multiprocessing.Manager finishes its execution, the Manager server gets shut down, which means your shared list object is now useless. This happens because the Manager object registers it's shutdown function as a "finalizer" with the multiprocessing module, which means it will be run just before the process exits. Here's the code that registers it, in BaseManager.__init__:

                      # register a finalizer
                      self._state.value = State.STARTED
                      self.shutdown = util.Finalize(
                          self, type(self)._finalize_manager,
                          args=(self._process, self._address, self._authkey,
                                self._state, self._Client),
                          exitpriority=0
                          )
                  

                  這是實際關閉的代碼:

                  @staticmethod
                  def _finalize_manager(process, address, authkey, state, _Client):
                      '''
                      Shutdown the manager process; will be registered as a finalizer
                      '''
                      if process.is_alive():
                          util.info('sending shutdown message to manager')
                          try:
                              conn = _Client(address, authkey=authkey)
                              try:
                                  dispatch(conn, None, 'shutdown')
                              finally:
                                  conn.close()
                          except Exception:
                              pass
                  
                          process.join(timeout=1.0)
                          if process.is_alive():
                              util.info('manager still alive')
                              if hasattr(process, 'terminate'):
                                  util.info('trying to `terminate()` manager process')
                                  process.terminate()
                                  process.join(timeout=0.1)
                                  if process.is_alive():
                                      util.info('manager still alive after terminate')
                  
                      state.value = State.SHUTDOWN
                      try:
                          del BaseProxy._address_to_local[address]
                      except KeyError:
                          pass
                  

                  解決方法很簡單 - 不要讓主進程立即完成,通過調用 server_process.join() 啟動運行 UDP 服務器的進程:

                  The fix is simple - don't let the main process complete immediately you start the process that runs the UDP server, by calling server_process.join():

                  import multiprocessing
                  from socketserver import UDPServer, ForkingMixIn, DatagramRequestHandler
                  from socket import socket, AF_INET, SOCK_DGRAM
                  from settings import host, port, number_of_connections
                  
                  class ChatHandler(DatagramRequestHandler):
                  
                      def handle(self):
                          cur_process = multiprocessing.current_process()
                          data = self.request[0].strip()
                          socket = self.request[1]
                          ChatHandler.clients.append(self.client_address) # error here
                          print(ChatHandler.clients)
                  
                  
                  class ChatServer(ForkingMixIn, UDPServer):
                      pass
                  
                  
                  if __name__ == '__main__':
                      server = ChatServer((host, port), ChatHandler)
                      ChatHandler.clients = multiprocessing.Manager().list()
                      server_process = multiprocessing.Process(target=server.serve_forever)
                      server_process.daemon = False
                      server_process.start()
                      server_process.join() # This fixes the issue.
                  

                  這篇關于在 python 服務器中的進程之間共享列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
                • <i id='XS0M7'><tr id='XS0M7'><dt id='XS0M7'><q id='XS0M7'><span id='XS0M7'><b id='XS0M7'><form id='XS0M7'><ins id='XS0M7'></ins><ul id='XS0M7'></ul><sub id='XS0M7'></sub></form><legend id='XS0M7'></legend><bdo id='XS0M7'><pre id='XS0M7'><center id='XS0M7'></center></pre></bdo></b><th id='XS0M7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='XS0M7'><tfoot id='XS0M7'></tfoot><dl id='XS0M7'><fieldset id='XS0M7'></fieldset></dl></div>
                    <tbody id='XS0M7'></tbody>

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

                        • <tfoot id='XS0M7'></tfoot>

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

                            <bdo id='XS0M7'></bdo><ul id='XS0M7'></ul>
                            主站蜘蛛池模板: 九九久久久| 九九九国产 | 久久久久久久久蜜桃 | 欧美成人一区二区三区 | 国产一区二区精品自拍 | 嫩草伊人 | 国产精品免费一区二区三区 | 久久99精品久久久久久 | 一区二区三区四区不卡视频 | 中文字幕一区二区三区精彩视频 | 国产伦精品一区二区三区高清 | 爱草在线 | 久久久久国产一区二区三区 | 91精品国产综合久久香蕉麻豆 | 日本久久网| 亚洲一区久久 | 午夜视频在线观看视频 | 亚洲国产精品99久久久久久久久 | 一区二区三区亚洲视频 | www视频在线观看 | 日韩亚洲欧美一区 | 久久国产免费 | 男女深夜网站 | 中文字幕在线观看一区二区 | 911影院| 福利视频二区 | 久久精品69 | 色在线看 | 99久久免费精品视频 | 国产一区二 | 日韩国产一区二区三区 | 日本成人中文字幕在线观看 | 欧美一区二区三区视频 | 亚洲激情专区 | 午夜在线影院 | 国产真实乱对白精彩久久小说 | 欧美xxxx色视频在线观看免费 | 亚洲精品国产成人 | 日韩欧美在线播放 | 亚洲人成网站777色婷婷 | 四虎影院一区二区 |