![](/img/trans.png)
[英]Pika connection lost Error: pika.exceptions.StreamLostError: Stream connection lost: ConnectionResetError(104, 'Connection reset by peer')
[英]Getting pika.exceptions.StreamLostError: Transport indicated EOF while running python script docker image which using pika
我正在使用 Python,它使用 RabbitMQ 作為輸入和 output。 我能夠在本地運行我的腳本而沒有任何錯誤,但是當我嘗試 Dockerize 該腳本並運行它時,它給了我以下錯誤:
Traceback (most recent call last):
File "./Kusto_connection_with_rabbitmq_2.py", line 1674, in <module>
main()
File "./Kusto_connection_with_rabbitmq_2.py", line 1668, in main
channel.start_consuming()
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 1865,
in start_consuming
self._process_data_events(time_limit=None)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 2026,
in _process_data_events self.connection.process_data_events(time_limit=time_limit)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 824,
in process_data_events
self._flush_output(common_terminator)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 523,
in _flush_output
raise self._closed_result.value.error
pika.exceptions.StreamLostError: Transport indicated EOF
下面是我的 Python 代碼,它連接到 RabbitMQ:
credentials = pika.PlainCredentials(username, password)
parameters = pika.ConnectionParameters(host=Api_url,virtual_host=rmqvhost,credentials=credentials,heartbeat=0)
print (username,password)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='test',durable=True)
channel.basic_qos(prefetch_size=0,prefetch_count=1) # this is for acknowdeging packet one by one
channel.basic_consume(queue='test', on_message_callback=callback,auto_ack=False)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
我的 Dockerfile:
FROM python:3.8
WORKDIR /First_try
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY Kusto_connection_with_rabbitmq_2.py .
CMD ["python","./Kusto_connection_with_rabbitmq_2.py"]
我運行我的 Docker 容器
docker run <image_name>
也許您的連接被中斷,而 pika 正在宣布您的客戶死亡。 嘗試將參數中的心跳設置為 30 左右。
我想您的問題是您無法從 docker-container 訪問 rabbitmq。
這是使用 docker-compose 的 python 中的 rabbitmq 使用的簡單示例,您應該嘗試依賴此示例來實現您的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.