How to prevent several employees from starting a task sent ONLY ONCE?

Recently, I noticed the strange behavior of Celery (3.1.25). The task is queued for execution using send_task() only once, however after a while I see that several launches of the same task! I spent hours looking at the celery documentation, trying to figure out how to prevent this behavior. Any help would be greatly appreciated!

Here is from inspect active :

 ... -> celery1@ec2-256-234-55-209.compute-1.amazonaws.com : OK * {'hostname': ' celery1@ec2-256-234-55-209.compute-1.amazonaws.com ', 'id': '5bf971b7-c2d2-47a1-9e3e-abec6c3c7ab4', 'args': "['myex', 'equities', 20170103]", 'time_start': 1633747.663716712, 'name': 'parsing.2pass', 'acknowledged': False, 'delivery_info': {'exchange': 'celery', 'priority': 0, 'redelivered': None, 'routing_key': 'celery'}, 'worker_pid': 28649, 'kwargs': '{}'} * {'hostname': ' celery1@ec2-256-234-55-209.compute-1.amazonaws.com ', 'id': '5bf971b7-c2d2-47a1-9e3e-abec6c3c7ab4', 'args': "['myex', 'equities', 20170103]", 'time_start': 1637348.143546186, 'name': 'parsing.2pass', 'acknowledged': False, 'delivery_info': {'exchange': 'celery', 'priority': 0, 'redelivered': None, 'routing_key': 'celery'}, 'worker_pid': 1550, 'kwargs': '{}'} -> celery1@ec2-54-234-55-254.compute-1.amazonaws.com : OK * {'hostname': ' celery1@ec2-256-234-55-254.compute-1.amazonaws.com ', 'id': '5bf971b7-c2d2-47a1-9e3e-abec6c3c7ab4', 'args': "['myex', 'equities', 20170103]", 'time_start': 1626395.204211438, 'name': 'parsing.2pass', 'acknowledged': False, 'delivery_info': {'exchange': 'celery', 'priority': 0, 'redelivered': None, 'routing_key': 'celery'}, 'worker_pid': 26978, 'kwargs': '{}'} -> celery1@ec2-54-226-20-88.compute-1.amazonaws.com : OK * {'hostname': ' celery1@ec2-256-226-20-88.compute-1.amazonaws.com ', 'id': '5bf971b7-c2d2-47a1-9e3e-abec6c3c7ab4', 'args': "['myex', 'equities', 20170103]", 'time_start': 1630146.08942695, 'name': 'parsing.2pass', 'acknowledged': False, 'delivery_info': {'exchange': 'celery', 'priority': 0, 'redelivered': None, 'routing_key': 'celery'}, 'worker_pid': 19473, 'kwargs': '{}'} ... 

Note that task 5bf971b7-c2d2-47a1-9e3e-abec6c3c7ab4 works for at least 3 workers, even if it was called by a single send_task () call. We use Redis as a broker with all the default values ​​(no interesting exchanges and routes).

+5
source share
1 answer

There may be several possible reasons.

  • You may have started using celery with celerybeat serving. And in this case there should be only one celery process. In other cases, each process will plan the same task.
  • Perhaps you should set up your queues. Because redis uses broadcast messages to perform tasks. Additional information here. Even if you do not use ETA / countdown, this can cause duplication.

In any case, you can prevent this on your side using celery_once . The main idea is to check inside the task if it has been advanced and completed. Yes, this seems like a workaround, but it works very well.

+1
source

Source: https://habr.com/ru/post/1261184/


All Articles