简体   繁体   English

芹菜-一秒钟完成一项任务

[英]Celery - one task in one second

I use Celery to make requests to the server (in tasks). 我使用Celery向服务器发出请求(在任务中)。 I have hard limit - only 1 request in one second (from one ip). 我有硬限制-一秒钟内只有1个请求(来自一个ip)。 I read this , so its what I want - 1/s. 我读了这个 ,所以它是我想要的-1 / s。 In celeryconfig.py I have: celeryconfig.py中,我有:

CELERY_DISABLE_RATE_LIMITS = False

CELERY_DEFAULT_RATE_LIMIT = "1/s"

But I have the messages, that I have too many requests per second. 但是我收到的消息是每秒请求太多。

In call.py I use groups . call.py中,我使用groups

I think, rate_limits does not work, because I have a mistake in celeryconfig.py . 我认为rate_limits不起作用,因为在celeryconfig.py中有一个错误。

How to fix that? 如何解决? Thanks! 谢谢!

When you start a celery worker with 当您开始做芹菜工作者时

celery -A your_app worker -l info

the default concurrency is equal to the number of the cores your machine has. 默认的并发性等于您的计算机具有的内核数。 So,eventhough you set a rate limit of '1/s', it is trying to process multiple tasks concurrently. 因此,即使将速率限制设置为“ 1 / s”,它仍试图同时处理多个任务。

Also setting a rate_limit in celery_config is a bad idea. 同样在rate_limit中设置rate_limit是一个坏主意。 Now you have only one task, if you add new tasks to your app, the rate limits will affect each other. 现在您只有一项任务,如果您向应用程序添加新任务,则速率限制将相互影响。

A simple way to achieve your one task per one second is this. 这是每秒完成一项任务的一种简单方法。

tasks.py task.py

import time

from  celery  import  Celery
app = Celery('tasks', backend='amqp', broker='amqp://guest@localhost//')

@app.task()
def task1():
    time.sleep(1)
    return('task1')

Now start you worker with a concurrency of ONE 现在开始并发ONE

celery -A my_taks.py worker -l info -c 1

This will execute only one task per second. 每秒仅执行一个任务。 Here is my log with the above code. 这是上面代码的日志。

[2014-10-13 19:27:41,158: INFO/MainProcess] Received task: task1[209008d6-bb9d-4ce0-80d4-9b6c068b770e]
[2014-10-13 19:27:41,161: INFO/MainProcess] Received task: task1[83dc18e0-22ec-4b2d-940a-8b62006e31cd]
[2014-10-13 19:27:41,168: INFO/MainProcess] Received task: task1[e1b25558-0bb2-405a-8009-a7b58bbfa4e1]
[2014-10-13 19:27:41,171: INFO/MainProcess] Received task: task1[2d864be0-c969-4c52-8a57-31dbd11eb2d8]
[2014-10-13 19:27:42,335: INFO/MainProcess] Task task1[209008d6-bb9d-4ce0-80d4-9b6c068b770e] succeeded in 1.170940883s: 'task1'
[2014-10-13 19:27:43,457: INFO/MainProcess] Task task1[83dc18e0-22ec-4b2d-940a-8b62006e31cd] succeeded in 1.119711205s: 'task1'
[2014-10-13 19:27:44,605: INFO/MainProcess] Task task1[e1b25558-0bb2-405a-8009-a7b58bbfa4e1] succeeded in 1.1454614s: 'task1'
[2014-10-13 19:27:45,726: INFO/MainProcess] Task task1[2d864be0-c969-4c52-8a57-31dbd11eb2d8] succeeded in 1.119111023s: 'task1'

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

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