【发布时间】:2023-01-16 06:39:53
【问题描述】:
我在 DigitalOcean 上部署了一个 FastAPI 应用程序,它有多个 API 端点,在其中一些端点中,我必须使用 RQ 包将抓取功能作为后台作业运行,以免让用户等待服务器响应。
我已经设法在 DigitalOcean 上创建了一个 Redis 数据库并成功地将应用程序连接到它,但是我在运行 RQ worker 时遇到了问题。 这是代码,灵感来自 RQ 的官方文档:
import redis
from rq import Worker, Queue, Connection
listen = ['high', 'default', 'low']
#connecting to DigitalOcean's redis db
REDIS_URL = os.getenv('REDIS_URL')
conn = redis.Redis.from_url(url=REDIS_URL)
#Create a RQ queue using the Redis connection
q = Queue(connection=conn)
with Connection(conn):
worker = Worker([q], connection=conn) #This instruction works fine
worker.work() #The deployment fails here, the DigitalOcean server crashes at this instruction
工作人员/工作执行在本地运行良好但在 DO 的服务器中失败 这可能是由于什么?有什么我遗漏的或需要在 DO 的端点上完成的任何类型的配置吗?
先感谢您!
我还尝试使用 FastAPI 的 BackgroundTask 类。起初,它运行顺利,但作业在中途停止运行,班级本身没有对后台发生的事情进行反馈。我猜这是由于在 FastAPI 中似乎没有自定义配置的超时(可能是因为它的后台任务本来就是低成本和快速的)。
我也在考虑尝试 Celery,但我担心我会遇到与 RQ 相同的问题。
【问题讨论】:
标签: python redis fastapi digital-ocean rq