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
相关产品推荐
相关产品推荐

