简体   繁体   English

在Celery中启动多个过程

[英]Starting multiple processes in Celery

I have a problem with configuring Celery for Django. 我在为Django配置Celery时遇到问题。

This is how I start django-celery: 这就是我开始django-celery的方法:

python manage.py celery worker --autoscale=10,2

Example of a task: 任务示例:

@task
def test(i):
    print "ITERATION {} START".format(i)
    time.sleep(10)
    print "ITERATION {} END".format(i)
    return True

And I call this task with: 我将此任务称为:

for i in range(10):
    test.delay(i)

What I expect to happen is that if I send 10 tasks to queue, 10 processes should open - one for each task. 我期望发生的事情是,如果我将10个任务发送到队列中,则应该打开10个进程-每个任务一个。

What actually happens is that the random number of processes are started, usually 4 and after these 4 tasks are finished , another 3 start and after they finish , another 3 start. 实际发生的情况是启动了随机数的进程,通常为4个, 在完成这4个任务后 ,又开始了3个, 在完成之后 ,又开始了3个。 This happens even for tasks that take longer to complete, eg 2 minutes. 即使完成时间较长(例如2分钟)的任务也会发生这种情况。

Can someone explain this behavior? 有人可以解释这种行为吗? How can I start all tasks immediately if autoscale upper limit allows it? 如果自动缩放上限允许,如何立即启动所有任务?

Also, though lower limit in autoscale is 2, when server is started, 3 processes run. 此外,尽管自动缩放​​的下限为2,但在启动服务器时,将运行3个进程。 Why is that? 这是为什么?

Platform: OpenWRT, Dual-Core processor, 2GB RAM. 平台:OpenWRT,双核处理器,2GB RAM。

Celery by default creates a worker per core, so I am assuming you are running on a machine with 4 cores. 默认情况下,Celery为每个内核创建一个工作线程,因此我假设您在具有4个内核的计算机上运行。 You can configure this using the flag --concurrency see documentation for further details. 您可以使用--concurrency标志进行配置,有关更多详细信息,请参见文档

After that you said 3 processes start and then 2 and so on, a task will start only once another has finished, and that also can be delayed sometimes because of the prefetch policy. 之后,您说启动了3个进程,然后启动了2个,依此类推,一个任务将仅在另一个任务完成后才启动,而且由于预取策略,有时也会延迟。 You can see this thread for more details. 您可以查看此线程以获取更多详细信息。

Celery starts a main process and X workers - the main process manages the workers, restarts them when needed and dispatches tasks to the workers. Celery启动一个主进程,X工人-主进程管理这些工人,在需要时重新启动它们,并将任务分派给工人。 Therefore if you have 2 workers - you will have 3 processes. 因此,如果您有2个工人-您将有3个工序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM