我尝试了很多东西,搜索了很多问题,但我的问题仍然没有解决。
我正在使用docker-compose
,python
celery
和rabbitmq
没有任何其他烧瓶或django框架。
我尝试了很多方法来配置芹菜工人池,我发现只有一个功能有效,但我无法使用 gui 实例运行它,并且在芹菜图像中使用它不起作用,app.control.grow_pool
我不是在用芹菜命令启动芹菜,而是用 python 文件。 https://docs.celeryproject.org/en/stable/userguide/configuration.html?highlight=autoscaler
# my_celery_app.py
from __future__ import absolute_import, unicode_literals
from celery.concurrency.prefork import TaskPool
from celery.worker.autoscale import Autoscaler
from celery import Celery
from analyzer.database_operator import get_db_all_tweet_list
import time
import os
user = os.getenv('LOGIN', 'admin')
password = os.getenv('PASSWORD', 'mypass')
hostname = os.getenv('HOSTNAME', 'localhost')
broker_url = f'amqp://{user}:{password}@{hostname}:5672/'
app = Celery('tasks', broker=broker_url, namespace="CELERY")
# app.conf.worker_pool = TaskPool
# app.control.worker_max_tasks_per_child = 10
# app.conf.worker_autoscaler = Autoscaler(None, min_concurrency=3, max_concurrency=10)
@app.task
def show(*args, **kwargs):
duration = 10
print(f"Start, args: {args}, kwargs: {kwargs}")
for x in range(duration):
# print(duration - x)
time.sleep(0.3)
# print("Bye celery")
get_db_all_tweet_list = app.task(get_db_all_tweet_list)
if __name__ == "__main__":
app.worker_main()
另一个不起作用的解决方案,但它引发了错误
# learning_celery.py
from celery.app.control import Control
controller = Control(app)
controller.autoscale(10, 4)
worker_1 | [2020-05-20 19:32:08,311: ERROR/MainProcess] pidbox command error: ValueError('Autoscale not enabled')
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.8/site-packages/kombu/pidbox.py", line 104, in dispatch
worker_1 | reply = handle(method, arguments)
worker_1 | File "/usr/local/lib/python3.8/site-packages/kombu/pidbox.py", line 126, in handle_cast
worker_1 | return self.handle(method, arguments)
worker_1 | File "/usr/local/lib/python3.8/site-packages/kombu/pidbox.py", line 120, in handle
worker_1 | return self.handlers[method](self.state, **arguments)
worker_1 | File "/usr/local/lib/python3.8/site-packages/celery/worker/control.py", line 513, in autoscale
worker_1 | raise ValueError('Autoscale not enabled')
worker_1 | ValueError: Autoscale not enabled
我想如果您想自己运行工作线程而不是使用提供的 CLI 工具,使用 app.worker_main((,那么您需要在argv
列表中提供参数。由于您主要关心自动缩放(据我所知已弃用 - 可惜,这是一个很好的功能(,因此至少应该["--autoscale", "10,3"]
argv .
因此,请尝试将app.worker_main()
线替换为app.worker_main(argv=["--autoscale", "10,3"])
,看看它是否有效......如果它不起作用,请尝试使用argv=["--autoscale=10,3"]
.