如何在celery中关闭连接以避免RabbitMQ中出现“客户端意外关闭TCP连接”警告

时间:2019-10-28 14:13:24

标签: rabbitmq celery

在运行以下简单的celery任务时,我总是在RabbitMQ日志输出中收到“客户端意外关闭TCP连接”警告。

from celery import Celery
import datetime, time
app = Celery('tasks',
            broker='amqp://guest@localhost/my_vhost'
            )

@app.task(ignore_result=True)
def foobar(name):
    with open('mylog.log', 'a') as logfile:
        try:
            nowstr = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
            logfile.write('[%s] start\n' % nowstr)
            logfile.flush()
            time.sleep(5)
            logfile.write(name + '\n')
            logfile.write('ended\n')
        except Exception as e:
            logfile.write('Exception: %s' % e)
        logfile.flush()

if __name__ == '__main__':
    foobar.delay('foobar')

我在以以下docker-compose开头的docker容器中运行RabbitMQ。

version: '2'
services:
  rabbitmq:
    image: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_VHOST=my_vhost
    ports: 
      - "5672:5672"

我开始的那个工人

celery -f celery.log -A sandbox worker --concurrency 1

如何正确关闭连接以避免这些警告?

0 个答案:

没有答案