You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Celery+Django+RabbitMQ时遇PermissionError及连接异常求助

Django + Celery + RabbitMQ: PermissionError(WinError 5) 及连接断开问题解决

核心问题

Windows环境下用Django结合Celery、RabbitMQ实现异步邮件发送时,触发PermissionError(13, 'Access is denied', None, 5, None),伴随RabbitMQ连接意外断开,即使以管理员权限运行CMD也无法解决,仅创建2个Celery任务就报错。

解决方法

1. 切换Celery并发模式为gevent

Windows系统下Celery默认的prefork模式(基于billiard进程池)易出现进程间同步权限问题,改用gevent协程模式可规避:

  • 安装gevent:
    pip install gevent
    
  • 启动Celery worker时指定并发模式:
    celery -A 你的项目名 worker -l info -P gevent
    

2. 调整Celery Worker启动参数

禁用不必要的节点发现和心跳机制,减少进程间通信冲突:

celery -A 你的项目名 worker -l info --without-gossip --without-mingle --without-heartbeat

3. 检查RabbitMQ服务状态与权限

  • 打开服务管理器(services.msc),确认RabbitMQ服务处于“正在运行”状态,未启动则手动启动。
  • 重置RabbitMQ用户权限(避免权限不足):
    rabbitmqctl add_user guest guest
    rabbitmqctl set_permissions -p / guest ".*" ".*" ".*"
    

4. 调整项目目录权限

确保项目所在目录(含虚拟环境)对当前用户有完全控制权限:
右键项目文件夹 → 属性 → 安全 → 编辑 → 选中当前用户 → 勾选“完全控制” → 应用并确定。

5. 临时禁用Windows安全软件

Windows Defender或第三方杀毒软件的实时保护可能拦截Celery进程间通信,临时禁用后重试任务,若问题解决,将项目目录添加到安全软件信任列表。

用户提供的错误日志

[2024-10-06 14:38:15,464: ERROR/SpawnPoolWorker-3] Pool process <billiard.pool.Worker object at 0x0000016C0BBF5A00> error: PermissionError(13, 'Access is denied', None, 5, None)
Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 473, in receive
    ready, req = _receive(1.0)
                 ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 445, in _recv
    return True, loads(get_payload())
                       ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\queues.py", line 394, in get_payload    with self._rlock:
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\synchronize.py", line 115, in __enter__
    return self._semlock.__enter__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 351, in workloop
    req = wait_for_job()
          ^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 480, in receive
    raise SystemExit(EX_FAILURE)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 292, in __call__
    sys.exit(self.workloop(pid=pid))
             ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 396, in workloop
    self._ensure_messages_consumed(completed=completed)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 406, in _ensure_messages_consumed
    if self.on_ready_counter.value >= completed:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 3, in getvalue
PermissionError: [WinError 5] Access is denied
[2024-10-06 14:38:15,464: ERROR/SpawnPoolWorker-3] Pool process <billiard.pool.Worker object at 0x0000016C0BBF5A00> error: PermissionError(13, 'Access is denied', None, 5, None)
Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 473, in receive
    ready, req = _receive(1.0)
                 ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 445, in _recv
    return True, loads(get_payload())
                       ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\queues.py", line 394, in get_payload    with self._rlock:
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\synchronize.py", line 115, in __enter__
    return self._semlock.__enter__()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 351, in workloop
    req = wait_for_job()
          ^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 480, in receive
    raise SystemExit(EX_FAILURE)
SystemExit: 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 292, in __call__
    sys.exit(self.workloop(pid=pid))
             ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 396, in workloop
    self._ensure_messages_consumed(completed=completed)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\billiard\pool.py", line 406, in _ensure_messages_consumed
    if self.on_ready_counter.value >= completed:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 3, in getvalue
PermissionError: [WinError 5] Access is denied
[2024-10-06 14:38:15,642: ERROR/MainProcess] Process 'SpawnPoolWorker-3' pid:19032 exited with 'exitcode 1'
[2024-10-06 14:38:16,108: INFO/SpawnPoolWorker-9] child process 160 calling self.run()
[2024-10-06 14:38:23,846: INFO/MainProcess] Events of group {task} enabled by remote.
[2024-10-06 14:41:05,702: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py", line 340, in start
    blueprint.start(self)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\bootsteps.py", line 116, in start
    step.start(parent)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py", line 746, in start
    c.loop(*c.loop_args())
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\loops.py", line 130, in synloop    connection.drain_events(timeout=2.0)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\kombu\connection.py", line 341, in drain_events
    return self.transport.drain_events(self.connection, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\kombu\transport\pyamqp.py", line 171, in drain_events
    return connection.drain_events(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\connection.py", line 526, in drain_events
    while not self.blocking_read(timeout):
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\connection.py", line 531, in blocking_read
    frame = self.transport.read_frame()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\transport.py", line 294, in read_frame
    frame_header = read(7, True)
                   ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\transport.py", line 637, in _read
    raise OSError('Server unexpectedly closed connection')
OSError: Server unexpectedly closed connection
[2024-10-06 14:41:05,780: WARNING/MainProcess] D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py:391: CPendingDeprecationWarning:
In Celery 5.1 we introduced an optional breaking change which
on connection loss cancels all currently executed tasks with late acknowledgement enabled.
These tasks cannot be acknowledged as the connection is gone, and the tasks are automatically redelivered
back to the queue. You can enable this behavior using the worker_cancel_long_running_tasks_on_connection_loss
setting. In Celery 5.1 it is set to False by default. The setting will be set to True by default in Celery 6.0.

  warnings.warn(CANCEL_TASKS_BY_DEFAULT, CPendingDeprecationWarning)

[2024-10-06 14:41:05,788: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2024-10-06 14:41:05,798: INFO/MainProcess] mingle: searching for neighbors
[2024-10-06 14:41:06,826: INFO/MainProcess] mingle: all alone
[2024-10-06 14:44:05,784: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
Traceback (most recent call last):
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py", line 340, in start
    blueprint.start(self)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\bootsteps.py", line 116, in start
    step.start(parent)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py", line 746, in start
    c.loop(*c.loop_args())
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\loops.py", line 130, in synloop    connection.drain_events(timeout=2.0)
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\kombu\connection.py", line 341, in drain_events
    return self.transport.drain_events(self.connection, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\kombu\transport\pyamqp.py", line 171, in drain_events
    return connection.drain_events(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\connection.py", line 526, in drain_events
    while not self.blocking_read(timeout):
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\connection.py", line 531, in blocking_read
    frame = self.transport.read_frame()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\transport.py", line 294, in read_frame
    frame_header = read(7, True)
                   ^^^^^^^^^^^^^
  File "D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\amqp\transport.py", line 637, in _read
    raise OSError('Server unexpectedly closed connection')
OSError: Server unexpectedly closed connection
[2024-10-06 14:44:33,538: WARNING/MainProcess] D:\study\learning-python\back-end\ecommerce\venv\Lib\site-packages\celery\worker\consumer\consumer.py:391: CPendingDeprecationWarning:
In Celery 5.1 we introduced an optional breaking change which
on connection loss cancels all currently executed tasks with late acknowledgement enabled.
These tasks cannot be acknowledged as the connection is gone, and the tasks are automatically redelivered
back to the queue. You can enable this behavior using the worker_cancel_long_running_tasks_on_connection_loss
setting. In Celery 5.1 it is set to False by default. The setting will be set to True by default in Celery 6.0.

  warnings.warn(CANCEL_TASKS_BY_DEFAULT, CPendingDeprecationWarning)

内容的提问来源于stack exchange,提问作者Abobaker khaled

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 13:38:09