core.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:CommunityCellularManager 作者: facebookincubator 项目源码 文件源码
def _send_and_receive(self, message):
    """Sending payloads to NM and returning Response instances.

    Or, if the action failed, an error will be raised during the instantiation
    of the Response.  Can also timeout if the socket receives no data for some
    period.

    Args:
      message: dict of a message to send to NM

    Returns:
      Response instance if the request succeeded

    Raises:
      TimeoutError: if nothing is received for the timeout
    """
    # zmq is thread unsafe: if we send a second request before
    # we get back the first response, we throw an exception
    # fix that -kheimerl
    with self.lock:
      # Send the message and poll for responses.
      self.socket.send(json.dumps(message))
      responses = self.socket.poll(timeout=self.socket_timeout * 1000)
      if responses:
        try:
          raw_response_data = self.socket.recv()
          return Response(raw_response_data)
        except zmq.Again:
          pass
      # If polling fails or recv failes, we reset the socket or
      # it will be left in a bad state, waiting for a response.
      self.socket.close()
      self.setup_socket()
      self.socket.connect(self.address)
      raise TimeoutError('did not receive a response')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号