記錄一下,
最近在用機器學習打算做一個Rest API, 數據存入mongo,任務采用消息隊列,rabbitmq
由於引擎采用python編寫,所以WEB也直接打算用python編寫了,比較省事。
WEB采用flask,連接rabbitmq使用pika,但是在鏈接過程中,無論是消費還是生產,只要過了一段時間就會主動斷鏈,
提示:
Connection reset by peer
其實原因很簡單,服務端沒有收到客戶端的心跳包,默認是10秒,但是預計的已經超過10秒還沒有發心跳包,所以服務端主動斷連了。
解決的辦法:
py2的代碼:
def __init__(self, # pylint: disable=R0913,R0914,R0912 host=_DEFAULT, port=_DEFAULT, virtual_host=_DEFAULT, credentials=_DEFAULT, channel_max=_DEFAULT, frame_max=_DEFAULT, heartbeat=_DEFAULT, ssl=_DEFAULT, ssl_options=_DEFAULT, connection_attempts=_DEFAULT, retry_delay=_DEFAULT, socket_timeout=_DEFAULT, locale=_DEFAULT, backpressure_detection=_DEFAULT, blocked_connection_timeout=_DEFAULT, client_properties=_DEFAULT, tcp_options=_DEFAULT, **kwargs): """Create a new ConnectionParameters instance. See `Parameters` for default values. :param str host: Hostname or IP Address to connect to :param int port: TCP port to connect to :param str virtual_host: RabbitMQ virtual host to use :param pika.credentials.Credentials credentials: auth credentials :param int channel_max: Maximum number of channels to allow :param int frame_max: The maximum byte size for an AMQP frame :param int heartbeat: Heartbeat timeout. Max between this value and server's proposal will be used as the heartbeat timeout. Use 0 to deactivate heartbeats and None to accept server's proposal. :param bool ssl: Enable SSL :param dict ssl_options: None or a dict of arguments to be passed to ssl.wrap_socket :param int connection_attempts: Maximum number of retry attempts :param int|float retry_delay: Time to wait in seconds, before the next :param int|float socket_timeout: Use for high latency networks :param str locale: Set the locale value :param bool backpressure_detection: DEPRECATED in favor of `Connection.Blocked` and `Connection.Unblocked`. See `Connection.add_on_connection_blocked_callback`. :param blocked_connection_timeout: If not None, the value is a non-negative timeout, in seconds, for the connection to remain blocked (triggered by Connection.Blocked from broker); if the timeout expires before connection becomes unblocked, the connection will be torn down, triggering the adapter-specific mechanism for informing client app about the closed connection ( e.g., on_close_callback or ConnectionClosed exception) with `reason_code` of `InternalCloseReasons.BLOCKED_CONNECTION_TIMEOUT`. :type blocked_connection_timeout: None, int, float :param client_properties: None or dict of client properties used to override the fields in the default client properties reported to RabbitMQ via `Connection.StartOk` method. :param heartbeat_interval: DEPRECATED; use `heartbeat` instead, and don't pass both :param tcp_options: None or a dict of TCP options to set for socket """
在py3:ConnectionParameters設置heartbeat_interval=0即可。
在py2:ConnectionParameters設置heartbeat=0即可。