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

FastAPI中ARQ Worker启动失败问题排查与解决方案

FastAPI中ARQ Worker启动问题的解决过程

问题1:FastAPI环境内直接启动Worker报错

直接调用Worker.run()时触发事件循环冲突错误:

RuntimeError: this event loop is already running

调用代码:

w = Worker(functions=[SMS.startSendingSms],redis_settings=redis_conn,on_startup = startup,max_jobs=1000,keep_result_forever=True,job_timeout=86000,max_tries=1000)
w.run()

问题2:Docker独立运行Worker触发断言错误

将Worker放到Docker容器独立运行时,出现函数未注册的断言错误:

AssertionError: at least one function or cron_job must be registered

即使已注册startSendingSms函数仍触发该错误。

最终解决配置与代码

Docker Compose配置

mxs-arq:
    container_name: mxs-arq
    build:
     context: ./backend
    volumes:
     - ./backend:/appINFO
    command: arq app.services.worker.worker.WorkerSettings
    restart: always
    depends_on:
     - redis

Python Worker配置代码

from dataclasses import dataclass
from arq import Worker, RedisSettings

@dataclass
class WorkerSettings:
    redis_pool = RedisSettings(host=settings.REDIS_DB,port=settings.REDIS_PORT,password=None)
     
w = Worker(functions=[SendingSmsTest],queue_name='messaging',redis_settings = redis_pool,max_jobs=1000,keep_result_forever=True,job_timeout=86000,max_tries=1000)
w.run()

操作步骤

  • 启动Docker容器
  • 执行create_pool命令
  • 入队任务

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 13:15:21