V
V
Vlad Koval2021-11-01 15:50:03
Django
Vlad Koval, 2021-11-01 15:50:03

How to implement sequential execution of Celery tasks?

I have a selery task that calls the XML parser of user profiles from folders in the database. The task is called when parsing each XML in the folder, but the tasks are executed in a chaotic order (asynchronously, I believe), which causes the problem of the user's photo, they are often incorrectly pulled up. Actually the question is how to perform tasks sequentially. Conventionally, one task has been taken and until it is completed, the next one will not start, or else make some kind of delay before execution. Yes, I lose in execution time, but it will be correct.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2021-11-03
@SevenEven22

Vlad Koval , I solved a similar problem as follows. I needed to write a quantity log in asynchronous tasks, but the log turned out to be something like this -> 4, 3, 5, 2, 1, -1, 0, etc. due to the fact that the tasks were performed in parallel. The solution is as follows, you need to create 4 (or as many as you need) queues default_0, default_1, default_2, default_3, since the processing was performed on a record in the database, then I had the id of this record. I took the remainder of dividing id % 4, where 4 is the number of queues, I got a number from 0 to 3 at the output, and depending on what number I got, I sent the task to that queue. Thus, work on a record with a specific id was always performed in a specific queue.
Of course, all these 4 queues need to be run in 1 instance

celery --app=conf.celery.application worker -c 1 -Q localhost_0
celery --app=conf.celery.application worker -c 1 -Q localhost_1
celery --app=conf.celery.application worker -c 1 -Q localhost_2
celery --app=conf.celery.application worker -c 1 -Q localhost_3

Tasks are queued like this
update_quantity_task.apply_async(
    kwargs={
        'goods_id': related_product.goods_id,
    },
    queue='default_{}'.format(related_product.goods_id % 4),
)

That's all, tasks are executed both in parallel and sequentially

D
Dmitry, 2021-11-01
@dmtrrr

https://docs.celeryproject.org/en/stable/userguide...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question