如何只为一个rq队列启动多个worker?

问题描述 投票:0回答:1

我只需要为我的一个队列(下面的“高”优先级)启动多个工作线程。如何在我用来启动工作人员的工作人员脚本的上下文中执行此操作?

from config import Config
from redis import from_url
from rq import Worker, Queue, Connection

listen = ['high','mid','low']

conn = from_url(Config.REDIS_URL)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(list(map(Queue, listen)),log_job_description=True,)
        worker.work()

工作脚本本身由主管进程调用,该进程为我的每个队列生成 2 个工作实例。


[supervisord]

[program:worker]
command=python -m worker
process_name=%(program_name)s-%(process_num)s
numprocs=2
directory=.
stopsignal=TERM
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
redirect_stderr=true

如果我想让 3 个工作人员为“高”队列做好准备,但只有 2 个工作人员为“中”和“低”队列做好准备,我该如何实现这一目标?

我尝试以“突发”模式启动工人,但如果没有足够的工作机会,这也会杀死工人。我可以接受一种解决方案,该解决方案可以像爆发一样自动扩展工作人员,但始终保持至少一个工作人员处于活动状态。

python-3.x python-rq
1个回答
0
投票

您可以修改工作文件以从命令行参数读取队列。 然后运行不同的survisor进程来管理不同类型的worker监听不同类型的队列。

from config import Config
from redis import from_url
from rq import Worker, Queue, Connection

try:
    listen = [sys.argv[1]]
except IndexError as e:
    listen = ['high','mid','low']

conn = from_url(Config.REDIS_URL)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(list(map(Queue, listen)),log_job_description=True,)
        worker.work()

主管脚本可能看起来像这样

[supervisord]

[program:high_worker]
command=python -m worker high
process_name=%(program_name)s-%(process_num)s
numprocs=1
directory=.
stopsignal=TERM
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
redirect_stderr=true

[program:worker]
command=python -m worker
process_name=%(program_name)s-%(process_num)s
numprocs=2
directory=.
stopsignal=TERM
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
redirect_stderr=true

这些脚本仅用于示例目的,您可能需要根据您的需要进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.