【问题标题】:How to run Python RQ jobs on a DigitalOcean application sever?如何在 DigitalOcean 应用程序服务器上运行 Python RQ 作业?
【发布时间】: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


    【解决方案1】:

    [Unit] Description=Gunicorn instance to serve myproject After=network.target

    [Service] User=devops Group=www-data WorkingDirectory=/home/user/myproject Environment="PATH=/home/user/myproject/myprojectvenv/bin" ExecStart=/home/user/myproject/myprojectvenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

    [program:rq_worker] command=/home/user/myproject/myprojectvenv/bin/rq -A rq_worker -l info directory=/home/user/myproject autostart=true autorestart=true stderr_logfile=/var/log/celery.err.log stdout_logfile=/var/log/celery.out.log

    [Install] WantedBy=multi-user.target

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 2015-05-09
      • 2014-06-21
      • 2016-12-09
      • 1970-01-01
      • 2020-04-11
      • 2017-11-22
      • 1970-01-01
      相关资源
      最近更新 更多