无法在redis中创建任务

zqry0prt  于 2021-06-07  发布在  Redis
关注(0)|答案(0)|浏览(233)

我正在使用celery 创建任务,但是过了一段时间redis返回一个错误

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/celery/worker/worker.py", line 203, in start
    self.blueprint.start(self)
  File "/usr/local/lib/python3.7/site-packages/celery/bootsteps.py", line 116, in start
    step.start(parent)
  File "/usr/local/lib/python3.7/site-packages/celery/bootsteps.py", line 365, in start
    return self.obj.start()
  File "/usr/local/lib/python3.7/site-packages/celery/worker/consumer/consumer.py", line 311, in start
    blueprint.start(self)
  File "/usr/local/lib/python3.7/site-packages/celery/bootsteps.py", line 116, in start
    step.start(parent)
  File "/usr/local/lib/python3.7/site-packages/celery/worker/consumer/connection.py", line 21, in start
    c.connection = c.connect()
  File "/usr/local/lib/python3.7/site-packages/celery/worker/consumer/consumer.py", line 400, in connect
    conn.transport.register_with_event_loop(conn.connection, self.hub)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/redis.py", line 1061, in register_with_event_loop
    cycle.on_poll_init(loop.poller)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/redis.py", line 336, in on_poll_init
    num=channel.unacked_restore_limit,
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/redis.py", line 201, in restore_visible
    self.unacked_mutex_expire):
  File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.7/site-packages/kombu/transport/redis.py", line 121, in Mutex
    lock_acquired = lock.acquire(blocking=False)
  File "/usr/local/lib/python3.7/site-packages/redis/lock.py", line 187, in acquire
    if self.do_acquire(token):
  File "/usr/local/lib/python3.7/site-packages/redis/lock.py", line 203, in do_acquire
    if self.redis.set(self.name, token, nx=True, px=timeout):
  File "/usr/local/lib/python3.7/site-packages/redis/client.py", line 1801, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python3.7/site-packages/redis/client.py", line 901, in execute_command
    return self.parse_response(conn, command_name,**options)
  File "/usr/local/lib/python3.7/site-packages/redis/client.py", line 915, in parse_response
    response = connection.read_response()
  File "/usr/local/lib/python3.7/site-packages/redis/connection.py", line 756, in read_response
    raise response
redis.exceptions.ReadOnlyError: You can't write against a read only replica.

据我所知,任务失败时就会出现这样的问题。需要注意的是,我使用的是docker compose。我附上docker-compose.yml的摘录:

web:
    build:
      context: .
      dockerfile: compose/django/Dockerfile
    restart: always
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"
    links:
      - redis
    env_file:
      - ./.envs/.django
    networks:
      - djangonetwork

redis:
    image: redis:alpine
    restart: always
    environment:
     - REDIS_REPLICATION_MODE=master
    ports:
      - "6379:6379"
    networks:
      - djangonetwork

networks:
  djangonetwork:
    driver: bridge

摘自settings.py:

CELERY_BROKER_URL = 'redis://redis:6379/0'
CELERY_RESULT_BACKEND = 'redis://redis:6379/0'

如何让celery 重新连接到redis没有任何问题?我是新来的,请帮忙。如果有必要,我可以补充这个问题

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题